初始化变量如何做下面的一行? 行执行后的$ B值为null如何实例化,并在单行
$b = ([System.Collections.Generic.Dictionary[string,object]]::new()).Add("dd",$null)
或
[System.Collections.Generic.Dictionary[string,object]]::new()).Add("dd",$null) -eq $null
True
初始化变量如何做下面的一行? 行执行后的$ B值为null如何实例化,并在单行
$b = ([System.Collections.Generic.Dictionary[string,object]]::new()).Add("dd",$null)
或
[System.Collections.Generic.Dictionary[string,object]]::new()).Add("dd",$null) -eq $null
True
($b = [System.Collections.Generic.Dictionary[string,object]]::new()).Add("dd",$null)
尝试hash tables而不是.NET的字典。这些是存储键 - 值对System.Collections.Hashtable
对象。在PowerShell中,你可以使用运营商@{}
创建一个哈希表,并能快速初始化一个上一行几个值。
你会有效地利用哈希表获得相同的行为,你会一本字典,也许除了一些性能上的差异,更noticably - 缺少你的价值观强类型的。
简单的演示:
PS C:> @{ "dd" = $null }
Name Value
---- -----
dd
从哈希表转换成字典在PowerShell中的一行:
$dictionary = New-Object 'System.Collections.Generic.Dictionary[String,object]'; (@{ "dd" = $null }).Keys | % { $dictionary.Add($_, $hashtable.Item($_)) }
'($ B = [系统.Collections.Generic.Dictionary [字符串对象] ::新())。加入( “DD”,$空)' – PetSerAl
代码不起作用,$ b为null –
P如果粘贴到PowerShell中,如果删除上面new()中的不可打印字符,estSerAI代码可以正常工作 –