我是一个初学者,并且知道C#适中.最近我在写这个power
shell脚本,想要创建一个Hashset.所以我写了($azAz是一个数组)
[System.Collections.Generic.HashSet[string]]$allset = New-Object System.Collections.Generic.HashSet[string]($azAZ)
并按下.我收到了这个消息:
New-Object : Cannot find an overload for "HashSet`1" and the argument count: "52". At filename.ps1:10 char:55 + [System.Collections.Generic.HashSet[string]]$allset = New-Object System.Collecti ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [New-Object],MethodException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
然后,我使用数组参数在PowerShell中使用googled构造函数,并将代码更改为:
[System.Collections.Generic.HashSet[string]]$allset = New-Object System.Collections.Generic.HashSet[string](,$azAZ)
不知怎的,我现在得到这个消息:
New-Object : Cannot find an overload for "HashSet`1" and the argument count: "1". At C:\Users\youngvoid\Desktop\test5.ps1:10 char:55 + [System.Collections.Generic.HashSet[string]]$allset = New-Object System.Collecti ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [New-Object],MethodException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
找不到HashSet和参数count 1的重载?你在跟我开玩笑吗?谢谢.
解决方法
这应该工作:
[System.Collections.Generic.HashSet[string]]$allset = $azAZ
更新:
要在构造函数中使用数组,数组必须是强类型的.这是一个例子:
[string[]]$a = 'one','two','three' $b = 'one','three' # This works $hashA = New-Object System.Collections.Generic.HashSet[string] (,$a) $hashA # This also works $hashB = New-Object System.Collections.Generic.HashSet[string] (,[string[]]$b) $hashB # This doesn't work $hashB = New-Object System.Collections.Generic.HashSet[string] (,$b) $hashB