PowerShell Invoke 返回数组而不是 int

Wou*_*ort 3 powershell

我有以下 PowerShell 片段:

$property = @{
    Getter = { 80 };
}

$value = $property.Getter.Invoke() 
$value.GetType() # Shows Name = Collection'1 instead of int
Run Code Online (Sandbox Code Playgroud)

我希望$value.GetType()返回Int32,但它返回一个集合。

我不能只获取元素[0],因为有时 Getter 函数会返回一个数组。

我怎样才能解决这个问题?

For*_*s1k 5

您可以通过这种方式严格声明返回值的类型。例如,返回类型将是Double

cls
$property = @{
    Getter = [Func[Double]]{ 80 }
}

$value = $property.Getter.Invoke() 
$value.GetType().Name
# Double
Run Code Online (Sandbox Code Playgroud)