【发布时间】:2021-03-04 20:11:23
【问题描述】:
在下面的代码中,我试图将哈希表传递给函数,但它总是将其类型更改为 Object[]
function Show-Hashtable{
param(
[Parameter(Mandatory = $true)][Hashtable] $input
)
return 0
}
function Show-HashtableV2{
param(
[Parameter(Mandatory = $true)][ref] $input
)
write-host $input.value.GetType().Name
return 0
}
[Hashtable]$ht=@{}
$ht.add( "key", "val" )
# for test
[int]$x = Show-HashtableV2 ([ref]$ht)
# main issue
[int]$x = Show-Hashtable $ht.clone()
上面我尝试了$ht.Clone()而不是$ht,但没有成功。
我得到了什么:
Object[]
Show-Hashtable : Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Collections.Hashtable".
At C:\_PowerShellRepo\Untitled6.ps1:26 char:11
+ [int]$x = Show-Hashtable $ht #.clone()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Show-Hashtable], PSInvalidCastException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException,Show-Hashtable
我在问看的方向。我的代码有什么问题?
【问题讨论】:
标签: powershell