Powershell Core 中的参考 .NET 通用嵌套类型
Reference .NET Generic Nested Type in Powershell Core
如标题所示,我正在尝试在 Powershell Core 中引用嵌套通用类型。
我发现使用 +
符号而不是 .
来访问 Powershell 中的嵌套类型...但语法似乎不适用于“嵌套通用类型” ...编译器不喜欢代码,语法错误。
Special use of plus (+) sign in Powershell
有没有人成功地让这个工作,或者这是一个已知的限制?
[System.Collections.Generic.Dictionary[[string], [int]]+Enumerator] GetEnumerator()
Daniel已提供解决方案:
[System.Collections.Generic.Dictionary`2+Enumerator[string, int]]
returns 感兴趣的类型(请注意,围绕单个泛型类型参数 string
和 int
的嵌套 [...]
是可选的)。
也就是说,在能够指定类型参数以便构造泛型类型之前,您必须首先指定它的open形式,需要在形式[=]中指定泛型arity(类型参数的计数,<n>
) 15=]跟在类型名称之后,即在System.Collections.Generic.Dictionary<TKey,TValue>
(expressed in C# notation), using the language-agnostic .NET API notation, as explained in .
的情况下是`2
- 如果不涉及nested类型(后缀为
+<typename>
),指定arity是optional if all type arguments已指定;例如,不用 [System.Collections.Generic.Dictionary`2[string, int]]
,[System.Collections.Generic.Dictionary[string, int]]
就足够了。
然而,有问题的枚举器类型 没有 public 构造函数 ,所以你不能直接实例化它;相反,它是在封闭类型 [System.Collections.Generic.Dictionary`2]
的(构造形式)上调用 .GetEnumerator()
时返回的类型;验证:
Get-Member -InputObject ([System.Collections.Generic.Dictionary[string, int]])::new().GetEnumerator()
如标题所示,我正在尝试在 Powershell Core 中引用嵌套通用类型。
我发现使用 +
符号而不是 .
来访问 Powershell 中的嵌套类型...但语法似乎不适用于“嵌套通用类型” ...编译器不喜欢代码,语法错误。
Special use of plus (+) sign in Powershell
有没有人成功地让这个工作,或者这是一个已知的限制?
[System.Collections.Generic.Dictionary[[string], [int]]+Enumerator] GetEnumerator()
Daniel已提供解决方案:
[System.Collections.Generic.Dictionary`2+Enumerator[string, int]]
returns 感兴趣的类型(请注意,围绕单个泛型类型参数 string
和 int
的嵌套 [...]
是可选的)。
也就是说,在能够指定类型参数以便构造泛型类型之前,您必须首先指定它的open形式,需要在形式[=]中指定泛型arity(类型参数的计数,<n>
) 15=]跟在类型名称之后,即在System.Collections.Generic.Dictionary<TKey,TValue>
(expressed in C# notation), using the language-agnostic .NET API notation, as explained in
`2
- 如果不涉及nested类型(后缀为
+<typename>
),指定arity是optional if all type arguments已指定;例如,不用[System.Collections.Generic.Dictionary`2[string, int]]
,[System.Collections.Generic.Dictionary[string, int]]
就足够了。
然而,有问题的枚举器类型 没有 public 构造函数 ,所以你不能直接实例化它;相反,它是在封闭类型 [System.Collections.Generic.Dictionary`2]
的(构造形式)上调用 .GetEnumerator()
时返回的类型;验证:
Get-Member -InputObject ([System.Collections.Generic.Dictionary[string, int]])::new().GetEnumerator()