gpt4 book ai didi

.net - Powershell 获取类型

转载 作者:行者123 更新时间:2023-12-02 18:55:47 26 4
gpt4 key购买 nike

我想检查是否存在某种类型。例如:

Add-Type -TypeDefinition '
public class Test{
public static int aaa(){
return 1;
}
}'

[test]::aaa() # 1

[type]::GetType('test') # empty

但是 GetType() 不适用于我的“测试”类型。

最佳答案

您可以测试该类型是否已存在

if (-not ('Test' -as [type])) {
# or
# if (-not ([System.Management.Automation.PSTypeName]'Test').Type) {
Add-Type -TypeDefinition '
public class Test{
public static int aaa(){
return 1;
}
}'
}

我不知道为什么 [type]::GetType('test') 不起作用,但是

("Test" -as [type])

确实有效并返回 $null 或

IsPublic IsSerial Name                                     BaseType                                                                                                               
-------- -------- ---- --------
True False Test System.Object

关于.net - Powershell 获取类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66199510/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com