4

コマンドを実行すると:

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

Powershell (バージョン 3) では、最終$var的に の値になり{System.Collections.DictionaryEntry, System.Collections.DictionaryEntry}ます。なぜこうなった?保存したい値を保存するにはどうすればよいですか?

4

1 に答える 1

5

これは、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オブジェクトの -property に保存されますが、それらKeyは使用した ID (a と b) です。

ハッシュテーブルを列挙する必要がある場合にのみ、これを気にする必要があります。すべてのアイテムをループしているとき。通常の使用では、これは舞台裏の魔法なので、使用できます$var["a"]

于 2016-03-18T18:07:58.403 に答える