2010-01-21 14 views
13

¿Cómo podría hacer una lista en PowerShell 2? He intentado éstos:
Los genéricos en PowerShell 2 no funcionan?

[activator]::createinstance(([type]'system.collections.generic.list`1').makegenerictype([string])) 

y

[activator]::createinstance(([type]'system.collections.generic.list`1').makegenerictype([string])) 

y todo lo que consigo es simplemente nada. ¿Qué está mal?

Estoy corriendo XP SP3, si importa

Respuesta

18

Prueba esto:

PS> $list = New-Object 'System.Collections.Generic.List[string]' 
PS> $list.Add('foo') 
PS> $list 
foo 

PS> $d = New-Object 'System.Collections.Generic.Dictionary[string,datetime]' 
PS> $d.Add('moonshot', [datetime]'7/20/1969') 
PS> $d['moonshot'] 

Sunday, July 20, 1969 12:00:00 AM 
+0

No puedo obtener este trabajo tampoco, no devuelve nada. ¿Debería funcionar? – Parsa

+0

Si tiene PowerShell 2.0 debería hacerlo. –

+0

Ahora, me di cuenta, estos intentos estaban bien, el problema era que el PS estaba mostrando los datos, por lo que no mostraría nada, gracias. – Parsa

3

Si intenta crear una lista basada en cadenas, intente esto:

New-Object 'System.Collections.Generic.List[system.string]' 

Tenga en cuenta que usted tiene que especificar 'system.string' (al menos en mi comp;)). Si solo usa 'cadena', arroja una excepción.

[61]: New-Object 'System.Collections.Generic.List[string]' 
New-Object : Cannot find type [System.Collections.Generic.List[string]]: make sure the assembly containing this type is loaded. 
At line:1 char:11 
+ New-Object <<<< 'System.Collections.Generic.List`1[string]' 
    + CategoryInfo   : InvalidType: (:) [New-Object], PSArgumentException 
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand 
+0

No hubo suerte, yo estaba usando esta sintaxis de PowerShell 1, pero ahora no devuelve nada en PS2, esta es la razón por la que trató de utilizar el activador clase. – Parsa

+0

¿Qué significa "no devuelve nada"? ¿cuál es el mensaje de error – stej

+2

Cuando la colección está vacía, no genera nada. Te da la impresión de que el nuevo objeto falló cuando en realidad funcionó. –

Cuestiones relacionadas