返回类型之谜

时间:2012-11-22 18:35:56

标签: 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 [,,],但突然变成对象[]在外面是奇特的。对于我正在使用的某些.Net库,我基本上需要一些Object [,,]类型的参数。

任何提示?

1 个答案:

答案 0 :(得分:3)

要了解发生了什么,请尝试输入:

PS C:\temp> $result[0]
Type in function is: System.Object[,,]
PS C:\temp> $result[1]
dummy-value

解释是函数放入数组时输出的所有内容。

要做你想做的事你必须写这个(不要忘记,在$结果之前):

function x { 
    $result = New-Object 'System.Object[,,]' 1,1,1 
    $result[0,0,0] = 'dummy-value'
    return ,$result    
}

然后:

PS C:\temp> $a = x
PS C:\temp> $a.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[,,]                               System.Array