2017-09-03 14 views
4

Powershell 2.0でAmazon.Cloudwatch.Model.Dimensionsタイプの新しいCollections.Generic.Listオブジェクトを作成しようとしています。Powershell 2でサードパーティーアセンブリタイプの汎用リストを宣言する

私は成功しAmazon.Cloudwatch.Model.Dimensionsオブジェクトを作成することができます

> $dimension = New-Object Amazon.Cloudwatch.Model.Dimensions 

そして私はとタイプstringCollections.Generic.Listオブジェクトを作成することができます:私がしようとするとのCollections.Generic.Listオブジェクトを作成するときに、しかし

> $list = New-Object Collections.Generic.List[string] 

Amazon.Cloudwatch.Model.Dimensionsと入力すると、次のエラーが表示されます。

> $list = New-Object Collections.Generic.List[Amazon.Cloudwatch.Model.Dimension] 

New-Object : Cannot find type 
[Collections.Generic.List[Amazon.Cloudwatch.Model.Dimension]]: make sure the assembly containing this type is loaded. 
At line:1 char:19 
+ $list = New-Object <<<< 
Collections.Generic.List[Amazon.Cloudwatch.Model.Dimension] 
    + CategoryInfo   : InvalidType: (:) [New-Object], 
PSArgumentException 
    + FullyQualifiedErrorId : 
TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand 

Powershell 3.0でも同じことが言えるので、Powershell 2.0ジェネリックがアセンブリをルックアップする方法が原因であるようです。 another SO answerによると関係している可能性のPowershell 2.0のバグがあります:

However, there's an unfortunate catch. In PowerShell 2.0, there's a bug when you mix and match BCL and 3rd party types as type parameters. The latter need to be assembly qualified:

# broken over two lines for clarity with backtick escape 
$o = new-object ('collections.generic.dictionary[[{0}],[{1}]]' -f ` 
    [type1].fullname, [type2].fullname) 

Hope this helps. In PowerShell 3.0, this has been fixed.

は、私は(一種の)成功せずにこれを試みてきました。私が試してみました

The solution is to specify the fully qualified assembly name for the generic parameter types. It's extremely ugly, but works:

$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]]" 

> $list = New-Object "System.Collections.Generic.List``1[[Amazon.CloudWatch.Model.Dimension, AWSSDK.CloudWatch, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]]" 

をも成功せず

そしてanother SO answerはしても、より多くのアセンブリ予選を示唆しています。

> $list = New-Object "System.Collections.Generic.List``1[[Amazon.CloudWatch.Model.Dimension, AWSSDK.CloudWatch, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]]" 
New-Object : Cannot find type 
[System.Collections.Generic.List`1[[Amazon.CloudWatch.Model.Dimension, AWSSDK.CloudWatch, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]]]: make sure the assembly containing this type is loaded. 
At line:1 char:22 
+ $dimlist = New-Object <<<< 
"System.Collections.Generic.List``1[[Amazon.CloudWatch.Model.Dimension, AWSSDK.CloudWatch, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604]]" 
    + CategoryInfo   : InvalidType: (:) [New-Object], PSArgumentException 
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand 

ご意見をいただければ幸いです。

注:Amazon.Cloudwatch.Model.Dimensions私は、このエンドツーエンドをテストすることはできませんよThe AWS SDK for .NET

答えて

2

これは、powershellバージョン2で動作することがテストされています。0は、あなたの質問ににリンクされている記事からのアイデアの組み合わせを使用した:

Add-Type -Path "C:\Program Files (x86)\AWS SDK for .NET\bin\Net35\AWSSDK.CloudWatch.dll" 
$o = "System.Collections.Generic.List``1" -as "Type" 
$o = $o.MakeGenericType("Amazon.Cloudwatch.Model.Dimension" -as "Type") 
$list = [Activator]::CreateInstance($o) 

$list.gettype() | select UnderlyingSystemType 

UnderlyingSystemType 
-------------------- 
System.Collections.Generic.List`1[Amazon.CloudWatch.Model.Dimension] 
+1

これを裸のWindows 7 VMでテストし、CLRバージョン2.0.50727.8669のPSH v.2.0で動作することを確認できます。 .net fx 4.0(これは必要ないと思います)とAWS SDK、最新バージョンもインストールしました。 – wp78de

+0

それに釘付け。乾杯! –

1

から来ています。確かに私は問題を確認することができます。

しかし、リストタイプを宣言し、それが飛ぶかどうかを見るときに、より一般的なものにしようとしないでください。

#Add-Type -Path "C:\Program Files (x86)\AWS SDK for .NET\bin\Net35\AWSSDK.CloudWatch.dll" 
$list = New-Object Collections.Generic.List[System.Object] 
$d = $dims = New-Object Amazon.Cloudwatch.Model.Dimension 
$d.name = "AutoScalingGroup" 
$d.value = "xy" 
$list.Add($dim) 
$list | Get-Member 

ここはスクリーンショットです。思考?もちろん

enter image description here

+1

追加の痛みを伴うキャストが必要になる可能性があるので、それは本当に飛ぶならば、私は知りませんが、私は、これも動作します確認することができます。 – wp78de

+0

サムに感謝、あなたの答えをupvotedしかし、上記のコメントに同意します。 –

0
$list = [Activator]::CreateInstance([System.Collections.Generic.List`1].MakeGenericType([Amazon.CloudWatch.Model.Dimension, AWSSDK.CloudWatch, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604])) 

あなたは前にアセンブリをロードしたり、GACに追加する必要があります。私のテストでは、すべてのアセンブリをC:\ Program Files(x86)\ AWS SDK for .NET \ bin \ Net45からロードしました。

foreach ($file in Get-ChildItem -Path 'C:\Program Files (x86)\AWS SDK for .NET\bin\Net35\' -Filter '*.dll') 
{ 
    [Reflection.Assembly]::LoadFile($file.FullName) | Out-Null 
} 
+1

これはPowerShellバージョン2に対して正しいですか?私は.net 4.5アセンブリをCLR2.0 – arco444

+0

に追加できませんでした。私は純粋なバージョン2.0を持っていないし、コマンド 'PowerShell.exe -Version '2.0でコンソールを起動しました。これはC:\ Program Files(x86)\ AWS SDK for .NET \ bin \ Net45のパスでは動作しませんが、C:\ Program Files(x86)\ AWS SDK for .NET \ bin \ Net35 "です。 – fdafadf

+1

残念ながら、このメソッドを使用してOPと同じエラーが発生します – arco444

関連する問題