2010-01-21 4 views
13

PowerShell 2에서 어떻게 목록을 만들 수 있습니까? 나는이 시도했다 :
PowerShell 2의 Generics가 작동하지 않습니까?

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

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


내가 얻을 모든 단지에 불과하다. 무슨 일있어?

당신이 문자열을 기반으로 목록을 만들려고하면이

답변

18

이 시도 : 나는 활성제를 사용하려고하는 이유

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

이 작업 중 하나를 수행 할 수 없으며 아무 것도 반환하지 않습니다. 작동해야합니까? – Parsa

+0

PowerShell 2.0을 사용하는 경우 사용해야합니다. –

+0

자,이 시도는 괜찮은 것 같았습니다. 문제는 PS가 데이터를 표시하고 있었기 때문에 아무 것도 표시하지 않았을 것입니다. 감사합니다. – Parsa

3

을 중요한 경우 나, XP SP3을 실행하는거야,이 시도 : 당신이 '선택 System.String'를 지정해야

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

주 (적어도 내 광고에;)). 방금 'string'을 사용하면 예외가 발생합니다.

[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

운, 내가 PowerShell을 1에서이 구문을 사용했지만, 지금은 PS2 아무것도 반환하지 않습니다,이 이유 수업. – Parsa

+0

"무엇이든 돌려주지 않는다"는 것은 무엇을 의미합니까? 오류 메시지는 무엇입니까 – stej

+2

컬렉션이 비어 있으면 아무 것도 출력하지 않습니다. 종류에 따라 새 객체가 실제로 작동 할 때 오류가 발생했다는 인상을 줄 수 있습니다. –

관련 문제