gpt4 book ai didi

.net - 在 Powershell 2 中声明第三方程序集类型的通用列表

转载 作者:行者123 更新时间:2023-12-01 01:49:05 27 4
gpt4 key购买 nike

我正在尝试创建一个新的 Collections.Generic.List类型对象 Amazon.Cloudwatch.Model.Dimensions在 Powershell 2.0 中。

我可以创建一个 Amazon.Cloudwatch.Model.Dimensions对象成功:

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

我可以创建一个 Collections.Generic.List类型对象 string和:
> $list = New-Object Collections.Generic.List[string]

但是,当我尝试创建一个 Collections.Generic.List 时类型对象 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.



我(有点)尝试过这个,但没有成功。

another SO answer建议通过以下方式进一步限定大会:

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

也无功而返。
> $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

最佳答案

这是在 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]

关于.net - 在 Powershell 2 中声明第三方程序集类型的通用列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46028686/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com