Powershel 中的泛型非常令人困惑。要实例化一个简单的列表,您需要手鼓跳舞:
$type = ("System.Collections.Generic.List"+'`'+"1") -as "Type"
$type= $type.MakeGenericType("System.string" -as "Type")
$o = [Activator]::CreateInstance($type)
但是如果我需要一些更复杂的东西怎么办:<Dictionary<string,List<Foo>>
例如
或者例如这里:Dictionary<string,List<string>>
$listType = ("System.Collections.Generic.List"+'`'+"1") -as "Type"
$listType = $listType.MakeGenericType("System.string" -as "Type")
$L = [Activator]::CreateInstance($listType)
$dicType = ("System.Collections.Generic.Dictionary"+'`'+"2") -as "Type"
#the next line is problematic
$dicType = $dicType.MakeGenericType(
@( ("system.string" -as "Type"),
("System.Collections.Generic.List" as "Type)) # and that's of course wrong
)
$D = [Activator]::CreateInstance($dicType )