powershell - 在运行时按名称获取通用类型
问题描述
我想知道为什么我不能用泛型定义检索类型,或者我能做些什么来实现这一点。
我的例子:
[System.Collections.Generic.List`1].FullName
--> Outputs: System.Collections.Generic.List`1
.
[System.Collections.Generic.List`1].AssemblyQualifiedName
--> Outputs: System.Collections.Generic.List`1, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
但两者
[System.Type]::GetType("System.Collections.Generic.List`1")
[System.Type]::GetType("System.Collections.Generic.List`1, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
即使在 mscorlib 中可以找到 List 的类型,也不要输出任何内容。有人可以解释一下如何按名称找到这种类型和其他泛型类型吗?
解决方案
是 PowerShell 中的`
转义字符,您需要在字符串中对其进行转义。这些中的任何一个都可以工作:
[System.Type]::GetType("System.Collections.Generic.List``1")
[System.Type]::GetType('System.Collections.Generic.List`1')