存储" System.Collections.DictionaryEntry"的值的Powershell哈希表。

时间:2016-03-18 17:32:04

标签: powershell powershell-v3.0 powergui

当我执行命令时:

$var = @{a=1;b=2}
在Powershell(第3版)中,$var的最终值为{System.Collections.DictionaryEntry, System.Collections.DictionaryEntry}。为什么会这样?如何存储我想要存储的值?

1 个答案:

答案 0 :(得分:4)

那是因为您的ISE枚举集合以创建变量树视图,而从HashtableEnumerator获得的$var.GetEnumerator()返回的对象是DictionaryEntry - 对象。

$var = @{a=1;b=2}

#Collection is a Hashtable
$var | Get-Member -MemberType Properties    

   TypeName: System.Collections.Hashtable

Name           MemberType Definition
----           ---------- ----------
Count          Property   int Count {get;}
IsFixedSize    Property   bool IsFixedSize {get;}
IsReadOnly     Property   bool IsReadOnly {get;}
IsSynchronized Property   bool IsSynchronized {get;}
Keys           Property   System.Collections.ICollection Keys {get;}  
SyncRoot       Property   System.Object SyncRoot {get;}               
Values         Property   System.Collections.ICollection Values {get;}

#Enumerated objects (is that a word?) are DictionaryEntry(-ies)
$var.GetEnumerator() | Get-Member -MemberType Properties

   TypeName: System.Collections.DictionaryEntry

Name  MemberType    Definition
----  ----------    ----------
Name  AliasProperty Name = Key
Key   Property      System.Object Key {get;set;}  
Value Property      System.Object Value {get;set;}

您的值(1和2)存储在对象的Value - 属性中,而Key是您使用的ID(a和b)。

当你需要枚举哈希表时,你只需要关心这个,例如。在循环浏览每个项目时。对于正常使用,这是幕后魔术,因此您可以使用$var["a"]