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 下使用哈希表,但我想知道如何通过上述声明创建字典。
我缺少什么?
Thx
使用的类型名称System.Collections.Generic.Dictionary[[string],[int]]
包含逗号。经过创建并初始化数组 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_arrays?view=powershell-5.1#creating-and-initializing-an-array:
要创建并初始化数组,请将多个值分配给
多变的。数组中存储的值是用 a 分隔
逗号…
因此,您需要转义逗号(阅读about_Escape_Characters https://github.com/juanpablojofre/About/blob/master/v3/Topic/about_Escape_Characters.md and about_引用_规则 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-5.1帮助主题)。还有更多选择:
在 Windows PowerShell 中,转义字符是backtick (`
),
也称为重音(ASCII 96)。
$dictionary = new-object System.Collections.Generic.Dictionary[[string]`,[int]]
引号用于指定文字字符串。您可以附上
中的一个字符串单引号 ('
) or 双引号
("
).
$dictionary = new-object "System.Collections.Generic.Dictionary[[string],[int]]"
or
$dictionary = new-object 'System.Collections.Generic.Dictionary[[string],[int]]'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)