gpt4 book ai didi

powershell - 返回类型之谜

转载 作者:行者123 更新时间:2023-12-03 01:21:18 25 4
gpt4 key购买 nike

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

有什么提示吗?

最佳答案

要了解发生了什么,只需尝试键入:

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

说明是将函数输出到数组的所有内容。

要执行您想要的操作,您必须编写以下代码(不要忘记$ result之前的):
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

关于powershell - 返回类型之谜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13518407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com