我是PowerShell的初学者,熟悉C#。最近我正在写这个PowerShell脚本,并想创建一个Hashset。所以我写了($ azAz是一个数组)从Powershell调用具有数组参数的构造函数
[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
然后,我用Google搜索构造函数在数组参数PowerShell和改变了代码:
[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的重载和参数计数1?你在跟我开玩笑吗?谢谢。
为什么逗号(,$阿扎兹)? – 2012-02-03 14:25:44
我不知道,我从谷歌搜索得到它。我甚至没有阅读过这篇文章,但至少它有把powershell作为1个参数对待$ azAZ的能力。也许这是因为逗号表示单独的论点? – irisjay 2012-02-03 14:51:04
这是因为逗号是数组创建操作符,所以它将$ azAZ变成一个单元素为$ azAZ的数组 - 我认为@($ azAZ)是创建一个数组的单元阵列的更清晰的方法。 – Massif 2012-02-03 15:04:28