代码之家  ›  专栏  ›  技术社区  ›  ca9163d9

Add Type不支持字典初始化语法?

  •  0
  • ca9163d9  · 技术社区  · 6 年前

    添加类型:c:\用户….cs(7):无效的表达式项“[”

    c: \Users…..cs(6):返回新字典{

    c: \Users…..cs(7):>>[“aa”]=“123”,

    c: \Users…..cs(8):[“bb”]=“235”

    $Source = @"
    using System.Collections.Generic;
    public class BasicTest
    {
      public static Dictionary<string, string> Add(int a, int b)
        {
            return new Dictionary<string, string>{ 
                ["aa"] = "123",
                ["bb"] = "235"
            };
        }
      public int Multiply(int a, int b)
        {
        return (a * b);
        }
    }
    "@
    
    Add-Type -TypeDefinition $Source
    
    0 回复  |  直到 6 年前
        1
  •  0
  •   parktomatomi    6 年前

    当我跑的时候 help Add-Type ,其中一个选项是:

    [-Language {CSharp | CSharpVersion3 | CSharpVersion2 | VisualBasic | JScript}]

    注意:我的Powershell版本是5.1,它是随Windows提供的,并早于当前的开源Powershell: https://github.com/PowerShell/PowerShell . 我敢打赌,您的示例将适用于更新的版本。

    推荐文章