首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell用字符串生成System.Type

Powershell用字符串生成System.Type
EN

Stack Overflow用户
提问于 2021-11-04 16:10:51
回答 1查看 107关注 0票数 3

我正在动态地生成某种类型的.net对象,比如这个$MyType=([System.Type]::GetType("System.String"),这对于标准.net对象来说很好。

我现在创建了这样一个自定义类

代码语言:javascript
复制
class InstanceName
{
    [String] hidden $Val
    InstanceName([String]$CVal)
    {
        $this.Val=$CVal
    }
}

在这里,此方法不起作用,因为powershell找不到类型。$MyType=([System.Type]::GetType("InstanceName")

知道如何获得自定义PS类的System.Type吗?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-04 16:14:54

使用-as类型转换操作符:

代码语言:javascript
复制
$instanceNameType = 'InstanceName' -as [type]

# test the type reference
$instanceNameType::new("")
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69842375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档