EBG*_*een 36 .net powershell powershell-1.0
我一直在推动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的一个问题.
小智 82
在PowerShell 2.0中,创建a的新方法Dictionary是:
$object = New-Object 'system.collections.generic.dictionary[string,int]'
Run Code Online (Sandbox Code Playgroud)
tom*_*asr 17
字典<K,V>未在与SortedDictionary <K,V>相同的程序集中定义.一个在mscorlib中,另一个在system.dll中.
这就是问题所在.PowerShell中的当前行为是,在解析指定的泛型参数时,如果类型不是完全限定的类型名称,则它假定它们与您尝试实例化的泛型类型位于同一程序集中.
在这种情况下,这意味着它在System.dll中寻找System.String,而不是在mscorlib中,因此它失败了.
解决方案是为通用参数类型指定完全限定的程序集名称.这非常难看,但有效:
$bar = new-object "System.Collections.Generic.Dictionary``2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39610 次 |
| 最近记录: |