【发布时间】:2019-11-13 09:22:34
【问题描述】:
PowerShell 版本:5.x、6
我正在尝试创建System.Collections.Generic.Dictionary 的新对象,但它失败了。
我尝试了以下“版本”:
> $dictionary = new-object System.Collections.Generic.Dictionary[[string],[int]]
New-Object : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ComObject'. Specified method is not supported.
At line:1 char:25
+ ... ry = new-object System.Collections.Generic.Dictionary[[string],[int]]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Object], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.NewObjectCommand
> $dictionary = new-object System.Collections.Generic.Dictionary[string,int]
New-Object : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ComObject'. Specified method is not supported.
At line:1 char:25
+ ... ionary = new-object System.Collections.Generic.Dictionary[string,int]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Object], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.NewObjectCommand
我知道我可以在 PowerShell 下使用哈希表,但我想知道如何通过上述声明创建字典。
我错过了什么?
谢谢
【问题讨论】:
-
转义
,逗号 使用反引号作为new-object System.Collections.Generic.Dictionary[[string]`,[int]],或使用引号作为new-object "System.Collections.Generic.Dictionary[[string],[int]]" -
@JosefZ,谢谢。有用。请添加您的评论作为答案。
标签: powershell