コマンドを実行すると:
$var = @{a=1;b=2}
Powershell (バージョン 3) では、最終$var
的に の値になり{System.Collections.DictionaryEntry, System.Collections.DictionaryEntry}
ます。なぜこうなった?保存したい値を保存するにはどうすればよいですか?
コマンドを実行すると:
$var = @{a=1;b=2}
Powershell (バージョン 3) では、最終$var
的に の値になり{System.Collections.DictionaryEntry, System.Collections.DictionaryEntry}
ます。なぜこうなった?保存したい値を保存するにはどうすればよいですか?
これは、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"]
。