谁能解释一下这种奇怪的 powershell 行为。
如果我声明以下内容:
$MyCollection = New-Object System.Collections.ArrayList
$MyCollection.Add(@{'Val1'=1; 'Val2'=2; 'Val3'=3})
$MyCollection.Add(@{'Val1'=1; 'Val2'=2; 'Val3'=3})
$MyCollection.Add(@{'Val1'=1; 'Val2'=2; 'Val3'=3})
所以......我应该有一个看起来像这样的对象:
System.Collections.Array.List
Index Item
0 {System.Collections.HashTable}
1 {System.Collections.HashTable}
2 {System.Collections.HashTable}
但是,如果我成为成员(member),您会看到我的整个收藏已变成一个大型哈希表
$MyCollection | Get-Member
TypeName: System.Collections.Hashtable
Name MemberType Definition
---- ---------- ----------
Add Method void Add(System.Object key, System.Object value), void IDictionary.Add(System.Object key, System.Object value)
Clear Method void Clear(), void IDictionary.Clear()
Clone Method System.Object Clone(), System.Object ICloneable.Clone()
Contains Method bool Contains(System.Object key), bool IDictionary.Contains(System.Object key)
ContainsKey Method bool ContainsKey(System.Object key)
ContainsValue Method bool ContainsValue(System.Object value)
CopyTo Method void CopyTo(array array, int arrayIndex), void ICollection.CopyTo(array array, int index)
Equals Method bool Equals(System.Object obj)
GetEnumerator Method System.Collections.IDictionaryEnumerator GetEnumerator(), System.Collections.IDictionaryEnumerator IDictionary.GetEnumerator(), System.Collections.IEnumerator IEnumerable.GetEn...
GetHashCode Method int GetHashCode()
GetObjectData Method void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context), void ISerializable.GetObjectData(System.Runtime....
GetType Method type GetType()
OnDeserialization Method void OnDeserialization(System.Object sender), void IDeserializationCallback.OnDeserialization(System.Object sender)
Remove Method void Remove(System.Object key), void IDictionary.Remove(System.Object key)
ToString Method string ToString()
Item ParameterizedProperty System.Object Item(System.Object key) {get;set;}
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;}
行为是这样的。例如,我无法按照我想要的方式访问我的对象:
$MyCollection | %{$_.Val1}
输出
1
1
1
预期输出
1
正如您所看到的,我们现在有一个大型哈希表,其行为非常奇怪。谁能解释一下 Powershell 实际上在做什么?因为它绝对不访问 ArrayList 集合内的 HashTable。
这就像调用任何 cmdlet 将我的数据结构扁平化为单个哈希表
最佳答案
当您通过管道传递数组时,它会展开,因此在您的示例中 Get-Member
可以看到数组的内部结构,即哈希表。
要获取数组对象的类型,您可以使用 GetType()
方法并查看 name 属性。
$MyCollection.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True ArrayList System.Object
Ansgar Wiechers 下面展示了您还可以使用 -InputObject
参数来获取数组的类型。
Get-Member -InputObject $MyCollection
如果您只需要哈希表数组输出中的单个项目,则需要指定数组中的哪个索引,您需要值后跟哈希表键。
$MyCollection[0]['Val1']
1
上面的示例返回存储在数组中第一个对象的键 Val1
中的值。要获得其他的,您必须增加索引号或更改 key 。
关于powershell - System.Collections.ArrayList 变为 System.Collecits.HashTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46105613/