Chr*_*ris 3 .net reflection powershell types
我正在动态生成某种类型的 .net 对象,如下所示$MyType=([System.Type]::GetType("System.String")
这对于标准 .net 对象来说效果很好。
我现在创建了一个像这样的自定义类
class InstanceName
{
[String] hidden $Val
InstanceName([String]$CVal)
{
$this.Val=$CVal
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,此方法不起作用,因为 powershell 无法找到类型。
$MyType=([System.Type]::GetType("InstanceName")
知道如何获取自定义 PS 类的 System.Type 吗?
谢谢!
使用-as类型转换运算符:
$instanceNameType = 'InstanceName' -as [type]
# test the type reference
$instanceNameType::new("")
Run Code Online (Sandbox Code Playgroud)