我想检查一下是否存在某种类型。例如:
Add-Type -TypeDefinition '
public class Test{
public static int aaa(){
return 1;
}
}'
[test]::aaa() # 1
[type]::GetType('test') # empty但是GetType()不适用于我的“test”类型。
发布于 2021-02-14 20:23:52
您可以测试类型是否已经存在于
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发布于 2021-02-16 15:14:27
补充西奥的有用答案,这无疑显示了最好的解决方案(-as [Type]),与为什么失败
[Type]::GetType()中动态定义类型时,它们是在动态、内存中的程序集中创建的,这些程序集是而不是,因此E 229 PowerShell E 133看不到它们E 234。请注意,这同样适用于:- Types created with [`Add-Type`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/add-type) `-TypeDefinition` or `-MemberDefinition`- Types created with the (PSv5+) `class` and `enum` keywords, i.e. custom PowerShell classes and enumerations.但是,[Type]::GetType()提供了重载功能,可以将自定义程序集-解析器功能连接到类型查找过程中,即在其他地方查找程序集中的类型。
这大概是PowerShell在幕后使用的(包括)在查找过程中只使用内存中的动态程序集的方法,它在以下上下文中起作用(一般情况下是不敏感的,比如PowerShell ):
[<typeName>]),因此您可以将您的Test类型称为[Test]。- Such type literals therefore also work with `-is`, the [type(-inheritance) / interface test operator](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Type_Operators), so you can test a given object for being of or deriving from the given type / implementing the given interface type.- Caveat: If the type name inside `[...]` isn't recognized as a loaded or dynamically declared type, an _exception_ occurs, which surfaces as a _statement-terminating error_ in PowerShell.-as**,的上下文中,[条件类型转换操作符**](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Type_Operators),如Theo's refers ('<typeName>' -as [type])中所示,[type]再次引用System.Type。- This approach tests if the string contains a type name that refers to a loaded type that can therefore be converted to a type-_information_ object.
- If so, such a type-information object is returned (the equivalent of `[Test]`, in your example); if not, `$null` is returned
- In a Boolean context such as an `if` conditional, a type-information object getting returned is implicitly `$true`, where as `$null` is implicitly `$false` - see the bottom section of [this answer](https://stackoverflow.com/a/53108138/45375) for the exact rules of implicit to-Boolean conversion), so the `-as [type]` approach is the most convenient way to test a type's presence.[type]:转换为时,类型名称字符串转换为 [type] 'Test'。- Unlike the `-as` approach, this approach triggers an _exception_ that surfaces as a _statement-terminating error_ in PowerShell.
- The (more verbose and less efficient) equivalent of `if ('Test' -as [type]) ...` isif ($(try { [type] 'Test' } catch { $null })) ...
关于名称空间的说明
class enum 和enum创建的类型具有无命名空间组件E 2106,因此仅以它们自己的名称引用类型就足够了,如D 107。Add-Type创建的类型- via **`-TypeDefinition`** (arbitrary C# code): - Whether you need a namespace qualifier depends on whether you enclose the new type's declaration in a `namespace <identifier> { ... }` construct or not.- via **`-MemberDefinition`** (C# code for declaring static methods, typically for [P/Invoke](https://en.wikipedia.org/wiki/Platform_Invocation_Services) calls): - You _do_ need a namespace qualifier to refer to the resulting type, namely either the one you've specified explicitly via the `-Namespace` parameter or the the implicitly used `Microsoft.PowerShell.Commands.AddType.AutoGeneratedTypes.WinApi` namespace. - You can bypass having to refer to the type by namespace-qualified name by using the `-PassThru` switch and saving the type-definition object in a variable, which allows you to call its static methods directly via that variable.https://stackoverflow.com/questions/66199510
复制相似问题