Generics PowerShell 2中的泛型不起作用?

Generics PowerShell 2中的泛型不起作用?,generics,powershell,powershell-2.0,Generics,Powershell,Powershell 2.0,我如何在PowerShell 2中列出一个列表? 我试过这些: [activator]::createinstance(([type]'system.collections.generic.list`1').makegenerictype([string])) 和 [activator]::createinstance(([type]'system.collections.generic.list`1').makegenerictype([string])) 我得到的只是一无所有。怎么了 我

我如何在PowerShell 2中列出一个列表? 我试过这些:

[activator]::createinstance(([type]'system.collections.generic.list`1').makegenerictype([string]))

[activator]::createinstance(([type]'system.collections.generic.list`1').makegenerictype([string]))
我得到的只是一无所有。怎么了


我正在运行XP SP3,如果这很重要

如果您尝试基于字符串创建列表,请尝试以下操作:

New-Object 'System.Collections.Generic.List[system.string]'
请注意,您必须指定“system.string”(至少在我的组件上)。如果只使用“string”,它会抛出一个异常

[61]: New-Object 'System.Collections.Generic.List[string]'
New-Object : Cannot find type [System.Collections.Generic.List[string]]: make sure the assembly containing this type is loaded.
At line:1 char:11
+ New-Object <<<<  'System.Collections.Generic.List`1[string]'
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
[61]:新对象“System.Collections.Generic.List[string]”
新对象:找不到类型[System.Collections.Generic.List[string]:请确保已加载包含此类型的程序集。
第1行字符:11
+新建对象尝试以下操作:

PS> $list = New-Object 'System.Collections.Generic.List[string]'
PS> $list.Add('foo')
PS> $list
foo

PS> $d = New-Object 'System.Collections.Generic.Dictionary[string,datetime]'
PS> $d.Add('moonshot', [datetime]'7/20/1969')
PS> $d['moonshot']

Sunday, July 20, 1969 12:00:00 AM

不走运,我在PowerShell 1中使用了此语法,但现在它在PS2中不返回任何内容,这就是我尝试使用Activator类的原因。它表示“它不返回任何内容”是什么意思?错误消息是什么当集合为空时,它不输出任何内容。给你的印象是新对象在实际工作时失败了。我也不能让这一个工作,什么也不返回。它应该工作吗?如果您使用的是PowerShell 2.0,它应该工作。现在,我发现,这些尝试都是正确的,问题是PS正在显示数据,所以它不会显示任何内容,谢谢。除非泛型类型驻留在不同的程序集中,否则这是有效的。然后我得到错误“确保加载了包含此类型的程序集”。加载类型程序集后,相同类型的新对象可以正常工作。