【发布时间】:2021-10-20 10:05:01
【问题描述】:
我有以下 PowerShell 的 sn-p:
$property = @{
Getter = { 80 };
}
$value = $property.Getter.Invoke()
$value.GetType() # Shows Name = Collection'1 instead of int
我希望$value.GetType() 返回Int32,但它会返回一个集合。
我不能只取元素 [0],因为有时 Getter 函数会返回一个数组。
我该如何解决这个问题?
【问题讨论】:
-
适用于防止展开的标准技巧:
(,$value)[0]将为您提供返回集合的第一个元素(并且它总是返回一个集合,因为这是ScriptBlock.Invoke()的签名),而无需急切地展开它,如果它是可枚举的。 -
这能回答你的问题吗? Why does invoking a Powershell script block with .Invoke() return a collection?:
$property.Getter.InvokeReturnAsIs().GetType()
标签: powershell