Tipo personalizzato in PowerShell con Generics Collection come membro
-
13-12-2019 - |
Domanda
Vorrei dichiarare il seguente tipo nel mio script PowerShell:
Add-Type @'
public class VirtualMachine {
string Name;
string HostName;
HashSet<string> Dependencies = new HashSet<string>();
}
'@
.
Ma il compilatore si lamenta di Hashset non può essere trovato.
Se commento la linea hashset, il tipo funziona bene.Immagino di dover dire a PowerShell di caricare il sistema. Assemblea ma non potevo capirlo.
Per favore, qualcuno può dirmi cosa mi manca?
thx in anticipo.
[modifica:] Con il tuo aiuto rapido, sono riuscito a risolvere il mio problema.THX per il tuo tempo.
Questo è il mio codice finale:
Add-Type -ReferencedAssemblies System.Core @"
using System.Collections.Generic;
public class VirtualMachine {
public string Name;
public string HostName;
public HashSet<string> Dependencies = new HashSet<string>();
}
"@
. Soluzione
In questo modo:
Add-Type -ReferencedAssemblies System.Core @"
using System.Collections.Generic;
public class VirtualMachine {
public string Name { get; set; }
public string HostName{ get; set; }
public HashSet<string> Dependencies { get; set; }
}
"@
.
È necessario aggiungere il riferimento di assembly a System.Core
e aggiungi un using System.Collections.Generic
in You C # codice.
Altri suggerimenti
È possibile aggiungere riferimenti tramite il parametro Assemblee di riferimento.
Add-Type -ReferencedAssemblies System.Core
.