数组 – 从Powershell调用具有数组参数的构造方法
发布时间:2020-05-24 09:28:45 所属栏目:Java 来源:互联网
导读:我是一个初学者,并且知道C#适中.最近我在写这个power shell脚本,想要创建一个Hashset.所以我写了($azAz是一个数组) [System.Collections.Generic.HashSet[string]]$allset = New-Object System.Collections.Generic.HashSet[string]($azAZ) 并按下.我收到了这
|
我是一个初学者,并且知道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:UsersyoungvoidDesktoptest5.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 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
