2012-02-03 12 views
11

Sono un principiante in PowerShell e conosco C# abbastanza bene. Recentemente stavo scrivendo questo script PowerShell e volevo creare un Hashset. Così ho scritto ($ azAz è un array)Chiamata al costruttore con Array Argomento da Powershell

[System.Collections.Generic.HashSet[string]]$allset = New-Object System.Collections.Generic.HashSet[string]($azAZ) 

e premuto corsa. Ho ricevuto questo messaggio:

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 

Poi, Ho cercato su google costruttori in PowerShell con parametri array e ha cambiato il codice per:

[System.Collections.Generic.HashSet[string]]$allset = New-Object System.Collections.Generic.HashSet[string](,$azAZ) 

In qualche modo, io ora ottenere questo messaggio:

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 

Non riesci a trovare un overload per HashSet e il conteggio argomento 1? Ma stai scherzando? Grazie.

+0

Perché la virgola in (, $ Azaz) ?? –

+0

non so, l'ho preso da una ricerca su google. Non ho nemmeno letto l'articolo, ma almeno ha ottenuto powershell per trattare $ azAZ come argomento 1. Forse è perché la virgola indica argomenti separati? – irisjay

+0

È perché la virgola è l'operatore di creazione dell'array, quindi rende $ azAZ in un array con un singolo elemento di $ azAZ - Penso che @ ($ azAZ) sia un modo più chiaro per creare un array di uno-array. – Massif

risposta

18

Questo dovrebbe funzionare:

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

UPDATE:

di utilizzare un array nel costruttore della matrice deve essere fortemente tipizzato. Ecco un esempio:

[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 
+0

grazie, ha funzionato. comunque, quale errore c'era nel mio codice? – irisjay

+0

Penso che l'inizializzazione delle raccolte fosse lo zucchero sintattico che è stato aggiunto nel c 3. Il compilatore prima crea la collezione, quindi aggiunge gli elementi dietro le quinte. PowerShell non ha la sintassi per questo. – Rynant

+0

ma la classe hashset contiene il costruttore HashSet (IEnumerable ) con 1 argomento, è sufficiente controllare msdn. – irisjay

1

provare in questo modo:

C:\> $allset = New-Object System.Collections.Generic.HashSet[string] 
C:\> $allset.add($azAZ) 
True 
+0

anche il metodo funziona, ma intendevo usare il costruttore di hashset. il tuo metodo però è bello ed elegante – irisjay

+0

wait, il tuo $ allset.add ($ azAZ) aggiunge tutti gli elementi in $ azAZ come 1 elemento! qualcosa è decisamente sbagliato qui. – irisjay

+0

Sì, il comando add() lo fa. Ho frainteso i tuoi bisogni. Il modo giusto per popolare HasSet dai valori di arry è nella risposta Rynant. –

Problemi correlati