我一直在推动PowerShell中的.NET框架,我遇到了一些我不理解的东西.这很好用:
$foo = New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"
$foo.Add("FOO", "BAR")
$foo
Key Value
--- -----
FOO BAR
Run Code Online (Sandbox Code Playgroud)
然而,这不是:
$bar = New-Object "System.Collections.Generic.SortedDictionary``2[System.String,System.String]"
New-Object : Cannot find type [System.Collections.Generic.SortedDictionary`2[System.String,System.String]]: make sure t
he assembly containing this type is loaded.
At line:1 char:18
+ $bar = New-Object <<<< "System.Collections.Generic.SortedDictionary``2[System.String,System.String]"
Run Code Online (Sandbox Code Playgroud)
他们都在同一个集会,所以我错过了什么?
正如答案中指出的那样,这只是PowerShell v1的一个问题.
例如,我有一个.NET对象,$m具有以下方法重载:
PS C:\Users\Me> $m.GetBody
OverloadDefinitions
-------------------
T GetBody[T]()
T GetBody[T](System.Runtime.Serialization.XmlObjectSerializer serializer)
Run Code Online (Sandbox Code Playgroud)
如果我尝试调用无参数方法,我得到:
PS C:\Users\Me> $m.GetBody()
Cannot find an overload for "GetBody" and the argument count: "0".
At line:1 char:1
+ $m.GetBody()
+ ~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Run Code Online (Sandbox Code Playgroud)
据我所知,PowerShell v3.0应该可以更容易地使用泛型.显然我需要告诉它我想要什么类型返回,但我无法弄清楚语法.