Powershell 返回式神秘
我在PowerShell中有如下内容Powershell 返回式神秘,powershell,return-type,Powershell,Return Type,我在PowerShell中有如下内容 function x { $result = New-Object 'System.Object[,,]' 1,1,1 'Type in function is: ' + $result.getType() $result[0,0,0] = 'dummy-value' return $result } $result = x $result.GetType() 奇怪的是,结果的类型在方法中是Object[,],
function x {
$result = New-Object 'System.Object[,,]' 1,1,1
'Type in function is: ' + $result.getType()
$result[0,0,0] = 'dummy-value'
return $result
}
$result = x
$result.GetType()
奇怪的是,结果的类型在方法中是Object[,],但在方法外部突然变成Object[]。对于我正在使用的某个.Net库,我基本上需要一些Object[,]类型的参数
有什么提示吗 要了解发生了什么,只需尝试键入:
PS C:\temp> $result[0]
Type in function is: System.Object[,,]
PS C:\temp> $result[1]
dummy-value
解释是函数放入数组后输出的所有内容
要做您想做的事情,您必须写以下内容(不要忘记,在$result之前):
然后:
PS C:\temp> $a = x
PS C:\temp> $a.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[,,] System.Array
谢谢!正是我想要的!那个神奇的逗号是干什么的?(a,b,c)给了我一个数组位,这在这里似乎不适用……PowerShell在函数返回数组时扩展数组。“,”只提供了一个要扩展的额外数组,因此我们收到了一个数组。谢谢。你说的额外数组是什么意思?提取数组?空数组。您能否详细说明一下“var”的语法?在PowerShell中,数组运算符是:
$a=1,2
创建一个包含两个整数的var$a
$a=1
创建一个包含一个整数的变量,这样您就可以创建一个包含一个整数的数组写入$a=,1
PS C:\temp> $a = x
PS C:\temp> $a.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[,,] System.Array