如果Get-Member没有列出它,散列表.Get_Item()和.Set_Item()如何工作?

Jas*_*Dev 3 powershell

我遇到了一篇Powershell哈希表文章,它使用.Get_Item()和.Set_Item().

两个问题:

1)允许.Get_Item()和.Set_Item()的语法工作的机制是什么?

2)可以运行哪些Powershell命令(如果有的话)来"发现"Get-Member未列出的额外属性和方法?

示例程序:

$h = @{}
$h | get-member
$h | get-member -static

$h.add("copper", 29)

$h["copper"]
$h.item("copper")
$h.get_item("copper")
Run Code Online (Sandbox Code Playgroud)

输出显示Item()但不包括Get_Item()和Set_Item():

   TypeName: System.Collections.Hashtable

Name              MemberType            Definition                                                            
----              ----------            ----------                                                            
Add               Method                System.Void Add(System.Object key, System.Object value)               
Clear             Method                System.Void Clear()                                                   
Clone             Method                System.Object Clone()                                                 
Contains          Method                bool Contains(System.Object key)                                      
ContainsKey       Method                bool ContainsKey(System.Object key)                                   
ContainsValue     Method                bool ContainsValue(System.Object value)                               
CopyTo            Method                System.Void CopyTo(array array, int arrayIndex)                       
Equals            Method                bool Equals(System.Object obj)                                        
GetEnumerator     Method                System.Collections.IDictionaryEnumerator GetEnumerator()              
GetHashCode       Method                int GetHashCode()                                                     
GetObjectData     Method                System.Void GetObjectData(System.Runtime.Serialization.Serializatio...
GetType           Method                type GetType()                                                        
OnDeserialization Method                System.Void OnDeserialization(System.Object sender)                   
Remove            Method                System.Void Remove(System.Object key)                                 
ToString          Method                string ToString()                                                     
Item              ParameterizedProperty System.Object Item(System.Object key) {get;set;}                      
Count             Property              System.Int32 Count {get;}                                             
IsFixedSize       Property              System.Boolean IsFixedSize {get;}                                     
IsReadOnly        Property              System.Boolean IsReadOnly {get;}                                      
IsSynchronized    Property              System.Boolean IsSynchronized {get;}                                  
Keys              Property              System.Collections.ICollection Keys {get;}                            
SyncRoot          Property              System.Object SyncRoot {get;}                                         
Values            Property              System.Collections.ICollection Values {get;}                          
    TypeName: System.Collections.Hashtable

Name            MemberType Definition                                                         
----            ---------- ----------                                                         
Equals          Method     static bool Equals(System.Object objA, System.Object objB)         
ReferenceEquals Method     static bool ReferenceEquals(System.Object objA, System.Object objB)
Synchronized    Method     static hashtable Synchronized(hashtable table)     
29
29
29
Run Code Online (Sandbox Code Playgroud)

Col*_*350 5

如果未指定cmdlet 的-force参数,则不会显示任何对象的get_和set_方法http://technet.microsoft.com/en-us/library/hh849928.aspx:get-member

-Force
将内在成员(PSBase,PSAdapted,PSObject,PSTypeNames)和编译器生成的get_和set_方法添加到显示中.默认情况下,Get-Member在除"Base"和"Adapted"之外的所有视图中获取这些属性,但它不显示它们