powershellmethodsreflectionpropertiesindexer

How does hash table .Get_Item() and .Set_Item() work if Get-Member doesn't list it?


I came across a Powershell hash table article and it used .Get_Item() and .Set_Item().

Two questions:

1) What's the mechanism that allows syntax of .Get_Item() and .Set_Item() to work?

2) What Powershell command (if any) can one run to "discover" extra properties and methods that's not listed by Get-Member?

Example program:

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

$h.add("copper", 29)

$h["copper"]
$h.item("copper")
$h.get_item("copper")

and the output shows Item() but neither Get_Item() nor 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

Solution

  • The get_ and set_ methods for any object will not be shown without specifying the -force parameter of the Get-Member cmdlet https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-member:

    -Force
    Adds the intrinsic members (PSBase, PSAdapted, PSObject, PSTypeNames) and the compiler-generated get_ and set_ methods to the display. By default, Get-Member gets these properties in all views other than "Base" and "Adapted," but it does not display them