programing

PowerShell 일반 컬렉션

sourcejob 2023. 8. 7. 22:30
반응형

PowerShell 일반 컬렉션

저는 계속 밀고 있습니다.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의 문제일 뿐입니다.

PowerShell 2.0의 새로운 생성 방법은Dictionary다음과 같습니다.

$object = New-Object 'system.collections.generic.dictionary[string,int]'

사전 <K,V>가 정렬된 사전 <K,V>와 동일한 어셈블리에 정의되어 있지 않습니다.하나는 mscorlib에 있고 다른 하나는 system.dll에 있습니다.

여기에 문제가 있습니다.PowerShell의 현재 동작은 지정된 일반 매개 변수를 해결할 때 유형이 전체 유형 이름이 아닌 경우 인스턴스화하려는 일반 유형과 동일한 어셈블리에 있다고 가정하는 것입니다.

이 경우 시스템을 찾고 있음을 의미합니다.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의 Generics에는 몇 가지 문제가 있습니다.PowerShell 팀의 개발자인 Lee Holmes는 Generics를 만들기 위해 이 스크립트를 게시했습니다.

언급URL : https://stackoverflow.com/questions/184476/powershell-generic-collections

반응형