"Mo"

Reflection by example with Powershell

Published on

Reflection by example with Powershell

Authors

A coworker swung by a few days ago to ask some questions about using Reflection. I learn really well by example so I decided to use Powershell to show using Reflection. Below is the session I used and later emailed to him. You can see a few spots at the bottom of the example where I was unsure of the syntax on passing an empty parameter array, but I figured it out.

This is a good example of why I like Powershell :-)

c:\> "haha".gettype()

IsPublic IsSerial Name BaseType
---- ---- -- ----
True True String System.Object

c:\> $temp = "haha".gettype()

c:\> $temp.GetProperties()

MemberType : Property
Name : Chars
DeclaringType : System.String
ReflectedType : System.String
MetadataToken : 385875994
Module : CommonLanguageRuntimeLibrary
PropertyType : System.Char
Attributes : None
CanRead : True
CanWrite : False
IsSpecialName : False

MemberType : Property
Name : Length
DeclaringType : System.String
ReflectedType : System.String
MetadataToken : 385875995
Module : CommonLanguageRuntimeLibrary
PropertyType : System.Int32
Attributes : None
CanRead : True
CanWrite : False
IsSpecialName : False

c:\> $temp.GetProperties()[0]

MemberType : Property
Name : Chars
DeclaringType : System.String
ReflectedType : System.String
MetadataToken : 385875994
Module : CommonLanguageRuntimeLibrary
PropertyType : System.Char
Attributes : None
CanRead : True
CanWrite : False
IsSpecialName : False

c:\> $temp.GetProperties()[0].name

Chars

c:\> $temp.GetProperties()[1].name

Length

c:\> $temp.GetProperties()[1].GetGetMethod()

Name : get_Length
DeclaringType : System.String
ReflectedType : System.String
MemberType : Method
MetadataToken : 100663629
Module : CommonLanguageRuntimeLibrary
MethodHandle : System.RuntimeMethodHandle
Attributes : PrivateScope, Public, HideBySig, SpecialName
CallingConvention : Standard, HasThis
ReturnType : System.Int32
ReturnTypeCustomAttributes : Int32
ReturnParameter : Int32
IsGenericMethod : False
IsGenericMethodDefinition : False
ContainsGenericParameters : False
IsPublic : True
IsPrivate : False
IsFamily : False
IsAssembly : False
IsFamilyAndAssembly : False
IsFamilyOrAssembly : False
IsStatic : False
IsFinal : False
IsVirtual : False
IsHideBySig : True
IsAbstract : False
IsSpecialName : True
IsConstructor : False

c:\> $temp.GetProperties()[1].GetGetMethod().Invoke

MemberType : Method
OverloadDefinitions : {System.Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo cu
lture), System.Object Invoke(Object obj, Object[] parameters)}
TypeNameOfValue : System.Management.Automation.PSMethod
Value : System.Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo cul
ture), System.Object Invoke(Object obj, Object[] parameters)
Name : Invoke
IsInstance : True

c:\> $temp.GetProperties()[1].GetGetMethod().Invoke("hello world", [])

Unable to find type []: make sure that the assembly containing this type is loaded.
At line:1 char:65
+ $temp.GetProperties()[1].GetGetMethod().Invoke("hello world", []) <<<<

c:\> $temp.GetProperties()[1].GetGetMethod().Invoke("hello world")

Cannot find an overload for "Invoke" and the argument count: "1".
At line:1 char:47
+ $temp.GetProperties()[1].GetGetMethod().Invoke( <<<< "hello world")

c:\> $temp.GetProperties()[1].GetGetMethod().Invoke("hello world", {})

Exception calling "Invoke" with "2" argument(s): "Parameter count mismatch."
At line:1 char:47
+ $temp.GetProperties()[1].GetGetMethod().Invoke( <<<< "hello world", {})

c:\> $temp.GetProperties()[1].GetGetMethod().Invoke("hello world", $Null)

11

c:\>