- Published on
Reflection by example with Powershell
- Authors
-
-
- Name
- David Mohundro
- Bluesky
- @david.mohundro.com
-
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 : PropertyName : CharsDeclaringType : System.StringReflectedType : System.StringMetadataToken : 385875994Module : CommonLanguageRuntimeLibraryPropertyType : System.CharAttributes : NoneCanRead : TrueCanWrite : FalseIsSpecialName : False
MemberType : PropertyName : LengthDeclaringType : System.StringReflectedType : System.StringMetadataToken : 385875995Module : CommonLanguageRuntimeLibraryPropertyType : System.Int32Attributes : NoneCanRead : TrueCanWrite : FalseIsSpecialName : False
c:\> $temp.GetProperties()[0]
MemberType : PropertyName : CharsDeclaringType : System.StringReflectedType : System.StringMetadataToken : 385875994Module : CommonLanguageRuntimeLibraryPropertyType : System.CharAttributes : NoneCanRead : TrueCanWrite : FalseIsSpecialName : False
c:\> $temp.GetProperties()[0].name
Chars
c:\> $temp.GetProperties()[1].name
Length
c:\> $temp.GetProperties()[1].GetGetMethod()
Name : get_LengthDeclaringType : System.StringReflectedType : System.StringMemberType : MethodMetadataToken : 100663629Module : CommonLanguageRuntimeLibraryMethodHandle : System.RuntimeMethodHandleAttributes : PrivateScope, Public, HideBySig, SpecialNameCallingConvention : Standard, HasThisReturnType : System.Int32ReturnTypeCustomAttributes : Int32ReturnParameter : Int32IsGenericMethod : FalseIsGenericMethodDefinition : FalseContainsGenericParameters : FalseIsPublic : TrueIsPrivate : FalseIsFamily : FalseIsAssembly : FalseIsFamilyAndAssembly : FalseIsFamilyOrAssembly : FalseIsStatic : FalseIsFinal : FalseIsVirtual : FalseIsHideBySig : TrueIsAbstract : FalseIsSpecialName : TrueIsConstructor : False
c:\> $temp.GetProperties()[1].GetGetMethod().Invoke
MemberType : MethodOverloadDefinitions : {System.Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture), System.Object Invoke(Object obj, Object[] parameters)}TypeNameOfValue : System.Management.Automation.PSMethodValue : System.Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture), System.Object Invoke(Object obj, Object[] parameters)Name : InvokeIsInstance : 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:\>