Array – call the constructor with array parameters from PowerShell
•
Java
I am a beginner and know c# moderation Recently, I was writing this power shell script and wanted to create a HashSet So I wrote ($azaz is an array)
[System.Collections.Generic.HashSet[string]]$allset = New-Object System.Collections.Generic.HashSet[string]($azAZ)
And press I received this message:
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
Then, I use the Google constructor in PowerShell using array parameters and change the code to:
[System.Collections.Generic.HashSet[string]]$allset = New-Object System.Collections.Generic.HashSet[string](,$azAZ)
Somehow, I got the news now:
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
Could not find overload for HashSet and parameter count 1? Are you kidding me? thank you.
Solution
This should work:
[System.Collections.Generic.HashSet[string]]$allset = $azAZ
to update:
To use arrays in constructors, arrays must be strongly typed Here is an example:
[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
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码