存储"System.Collections.DictionaryEntry"值的PowerShell哈希表 [英] Powershell hash table storing values of "System.Collections.DictionaryEntry"

查看:11
本文介绍了存储"System.Collections.DictionaryEntry"值的PowerShell哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行命令时:

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

在PowerShell(版本3)中,$var以值{System.Collections.DictionaryEntry, System.Collections.DictionaryEntry}结束。为什么会发生这种事?如何存储要存储的值?

推荐答案

这是因为您的ISE正在枚举集合以创建变量-treeview,而从$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"]

这篇关于存储"System.Collections.DictionaryEntry"值的PowerShell哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆