PowerShell 可以not有联合数据类型,但您可以使用 PowerShell 的default数组,它们是[object[]]
-类型化,因此可以包含以下元素any type:
# Note: @(...) around the array isn't strictly necessary.
$arrIntOrString = 1, 'two', 3
没有直接的方法limit允许哪些特定类型。
但是,您可以使用验证属性- 类型System.Management.Automation.ValidateScriptAttribute https://learn.microsoft.com/en-US/dotnet/api/System.Management.Automation.ValidateScriptAttribute在本例中 - 强制将元素限制为指定类型:
[ValidateScript({ $_ -is [int] -or $_ -is [string] })] $arrIntOrString = 1, 'two', 3
The above would enforce the specified types both on initial assignment and later modifications; e.g., the following attempt to later "add"[1] an element of a non-permitted type would fail:
# FAILS, because $true (of type [bool]) is neither an [int] nor [string]
$arrIntOrString += $true
不幸的是,错误消息有点晦涩:MetadataError: The variable cannot be validated because the value System.Object[] is not a valid value for the arrIntOrString variable.
请注意,此验证是公平的slow,假设有一个脚本块({ ... }
) 必须执行对于每个元素.
您还可以将此技术应用于参数声明,其中在PowerShell(核心)7+允许您定义自定义错误消息:
Function Foo {
param(
# Note: Use of ErrorMessage requires PS 7+
[ValidateScript({ $_ -is [int] -or $_ -is [string] }, ErrorMessage = 'Please pass an integer or a string.')]
$IntOrString
)
$IntOrString
}
Foo -IntOrString 1.0 # Fails validation
[1] Arrays are fixed-length data structures, so what PowerShell does when you "add" to an array with +=
is to create a new array behind the scenes, with the new element(s) appended. Efficiently extensible alternatives to arrays are the non-generic System.Collections.ArrayList https://learn.microsoft.com/en-US/dotnet/api/System.Collections.ArrayList type (e.g., [System.Collections.ArrayList] (1, 'two', 3)
) and the generic System.Collections.Generic.List`1 https://learn.microsoft.com/en-US/dotnet/api/System.Collections.Generic.List-1 type (e.g., [System.Collections.Generic.List[object]] (1, 'two', 3)
). Then use the .Add()
method to add to these collections; note that the ArrayList.Add()
method returns a value, which you can suppress with $null = ...
. To initialize an empty collection of either type, cast @()
to the type literal or call its static ::new()
method.