gpt4 book ai didi

arrays - 从 Powershell 使用数组参数调用构造函数

转载 作者:行者123 更新时间:2023-12-04 01:42:57 29 4
gpt4 key购买 nike

我是 powershell 的初学者,对 C# 有一定的了解。最近我在写这个 powershell 脚本,想创建一个 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 中搜索构造函数并将代码更改为:
[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?你在逗我吗?谢谢。

最佳答案

这应该有效:

[System.Collections.Generic.HashSet[string]]$allset = $azAZ

更新:

要在构造函数中使用数组,数组必须是强类型的。下面是一个例子:
[string[]]$a = 'one', 'two', 'three'
$b = 'one', 'two', '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

关于arrays - 从 Powershell 使用数组参数调用构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9130045/

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