PowerShell 일반 컬렉션
-
06-07-2019 - |
문제
나는 PowerShell의 .NET 프레임 워크를 밀고 있었고 이해하지 못하는 것을 쳤다. 이것은 잘 작동합니다 :
$foo = New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"
$foo.Add("FOO", "BAR")
$foo
Key Value
--- -----
FOO BAR
그러나 이것은 다음과 같습니다.
$bar = New-Object "System.Collections.Generic.SortedDictionary``2[System.String,System.String]"
New-Object : Cannot find type [System.Collections.Generic.SortedDictionary`2[System.String,System.String]]: make sure t
he assembly containing this type is loaded.
At line:1 char:18
+ $bar = New-Object <<<< "System.Collections.Generic.SortedDictionary``2[System.String,System.String]"
둘 다 같은 조립품에 있습니다. 그래서 내가 무엇을 놓치고 있습니까?
답변에서 지적한 바와 같이, 이것은 PowerShell V1의 문제 일뿐입니다.
해결책
사전u003CK,V> SortedDictionary와 동일한 어셈블리에서 정의되지 않습니다u003CK,V> . 하나는 mscorlib에 있고 다른 하나는 System.dll에 있습니다.
거기에는 문제가 있습니다. PowerShell의 현재 동작은 지정된 일반 매개 변수를 해결할 때 유형이 완전히 적격 유형 이름이 아닌 경우 인스턴스화하려는 일반 유형과 동일한 어셈블리에 있다고 가정합니다.
이 경우 System.String에서 System.String을 찾고 있음을 의미하며 mscorlib가 아닌 System.dll이 없으므로 실패합니다.
솔루션은 일반 매개 변수 유형에 대한 정규화 된 어셈블리 이름을 지정하는 것입니다. 매우 못 생겼지 만 작동합니다.
$bar = new-object "System.Collections.Generic.Dictionary``2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"
다른 팁
PowerShell 2.0에서 새로운 방법은 a Dictionary
이다:
$object = New-Object 'system.collections.generic.dictionary[string,int]'
PowerShell에는 제네릭과 관련하여 몇 가지 문제가 있습니다. Lee Holmes, PowerShell 팀의 개발자 게시물 이 스크립트 제네릭을 만들려면.
제휴하지 않습니다 StackOverflow