.net PowerShell 通用集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/184476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:16:39  来源:igfitidea点击:

PowerShell generic collections

.netpowershellpowershell-1.0

提问by EBGreen

I have been pushing into the .NET framework in PowerShell, and I have hit something that I don't understand. This works fine:

我一直在 PowerShell 中推进 .NET 框架,但遇到了一些我不明白的事情。这工作正常:

$foo = New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"
$foo.Add("FOO", "BAR")
$foo

Key                                                         Value
---                                                         -----
FOO                                                         BAR

This however does not:

然而,这不会:

$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]"

They are both in the same assembly, so what am I missing?

他们都在同一个程序集中,所以我错过了什么?

As was pointed out in the answers, this is pretty much only an issue with PowerShell v1.

正如答案中指出的那样,这几乎只是 PowerShell v1 的一个问题。

采纳答案by tomasr

Dictionary<K,V> is not defined in the same assembly as SortedDictionary<K,V>. One is in mscorlib and the other in system.dll.

Dictionary<K,V> 未在与 SortedDictionary<K,V> 相同的程序集中定义。一个在 mscorlib 中,另一个在 system.dll 中。

Therein lies the problem. The current behavior in PowerShell is that when resolving the generic parameters specified, if the types are not fully qualified type names, it sort of assumes that they are in the same assembly as the generic type you're trying to instantiate.

这就是问题所在。PowerShell 中的当前行为是,在解析指定的泛型参数时,如果类型不是完全限定的类型名称,它会假定它们与您尝试实例化的泛型类型在同一程序集中。

In this case, it means it's looking for System.String in System.dll, and not in mscorlib, so it fails.

在这种情况下,这意味着它正在 System.dll 中而不是在 mscorlib 中查找 System.String,因此它失败了。

The solution is to specify the fully qualified assembly name for the generic parameter types. It's extremely ugly, but works:

解决方案是为泛型参数类型指定完全限定的程序集名称。它非常丑陋,但有效:

$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]]"

回答by ShanePowser

In PowerShell 2.0 the new way to create a Dictionaryis:

在 PowerShell 2.0 中,创建 a 的新方法Dictionary是:

$object = New-Object 'system.collections.generic.dictionary[string,int]'

回答by Steven Murawski

There are some issues with Generics in PowerShell. Lee Holmes, a dev on the PowerShell team posted this scriptto create Generics.

PowerShell 中的泛型存在一些问题。PowerShell 团队的开发人员 Lee Holmes 发布了这个脚本来创建泛型。