检查一个变量是否是字符串列表/数组?

huangapple go评论58阅读模式
英文:

Check if a variable is a string list/array?

问题

以下代码返回True。在PowerShell中正确检查类型的方式是什么?

$e = @(1,2)
$e -ne $null -and $e.GetType() -eq (@("")).GetType()

更新:

以下代码将返回False。预期结果应为True

$e = @('1','2')
$e -is [string[]]
英文:

The following code returns True. What's the correct way to check the type in PowerShell?

$e = @(1,2)
$e -ne $null -and $e.GetType() -eq (@("")).GetType()

Update:

The following code will return false. It's expected to be true.

$e = @('1','2')
$e -is [string[]]

答案1

得分: 3

您可以使用-is运算符来检查类型,以确定它是否为数组:

$arr = 1, 2
$arr -is [array]

然而,更加谨慎的方法可能是检查对象是否实现了IEnumerable接口,因为有许多集合类型不一定是数组,但是可以进行迭代:

$arr = 1, 2
$arr -is [System.Collections.IEnumerable]

还请注意,如果将cmdlet/function的结果分配给一个变量,该结果可以返回多个元素,但默认情况下,如果返回的集合只包含一个元素,它将被解开并分配为单个值。您可以使用以下技巧之一来强制返回的集合被评估/分配为一个数组(以Get-Process作为示例):

$arr = @( Get-Process iexplore ) # 强制返回值作为集合的一部分,即使只返回单个进程。
$arr -is [System.Collections.IEnumerable] # true

或者,您还可以使用,符号来强制返回的值被评估为集合(在这种情况下,您需要在cmdlet/function调用中使用子表达式):

$arr = , ( Get-Process iexplore )
$arr -is [System.Collections.IEnumerable]

如果您要检查一个泛型集合(其类型定义在System.Collections.Generic命名空间中),您还可以使用System.Collections.Generic.IEnumerable[T]来检查与集合相关联的类型,其中T是数组中允许的元素类型。例如:

$list = New-Object System.Collections.Generic.List[string]
$list -is [System.Collections.Generic.IEnumerable[string]] # true
英文:

You can use the -is operator to check the type to check if it's an array:

$arr = 1, 2
$arr -is [array]

However, it is probably more prudent to check that the object implements IEnumerable, since there are many collection types that are not specifically arrays but are iterable:

$arr = 1, 2
$arr -is [System.Collections.IEnumerable]

Also note that if you assign the result of a cmdlet/function to a variable that can return more than one element, but it only returns a collection with a single element, by default it will be unrolled and assigned as a single value. You can force a returned collection to be evaluated/assigned as an array with one of the following techniques (using Get-Process as an example of this):

$arr = @( Get-Process iexplore ) # ===========> Forces the returned value to be part of a collection,
                                 #              even if only a single process is returned.
$arr -is [System.Collections.IEnumerable] # ==> true

or you can use the , notation to force the returned values to be evaluated as a collection as well (you will need to sub-express your cmdlet/function call in this case):

$arr = , ( Get-Process iexplore )
$arr -is [System.Collections.IEnumerable]

If you are checking a generic collection (a collection whose type is defined within the System.Collections.Generic namespace), you can also check against the collection's associated type using System.Collections.Generic.IEnumerable[T] instead, where T is the type of element allowed in the array. For example:

$list = New-Object System.Collections.Generic.List[string]
$list -is [System.Collections.Generic.IEnumerable[string] # ==> true

huangapple
  • 本文由 发表于 2020年1月7日 01:27:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定