Jump to content
Xtreme .Net Talk

Recommended Posts

  • Leaders
Posted (edited)

Anyone who has spent more than a little bit of time getting their hands dirty with reflection will be well aware that all kinds of magic can happen through the System.Type.InvokeMember method. Unfortunately, though, this function has a large number of parameters to cover nearly every member invocation/access scenario and it can become tedious stringing together confusing parameter arrays and binding flags and determining which parameters are relevant for every invocation, and the resulting code is very difficult to read.

 

The Reflector class provides a number of methods that expose .Net's reflection capabilities in a more intuitive manner (and extends that functionality). Numerous overloads are provided for several clearly named functions that perform a specific type of reflection (method invocation, property getting, field setting, etc.). Additionally, the Reflector class uses a cut-down version of my Subscriber class to enable event handling through reflection.

 

Consider the following approximately equivalent code listings:

// Using standard reflection
Type myType = this.GetType();

// Change form's text
myType.InvokeMember("Text",
   System.Reflection.BindingFlags.FlattenHierarchy |
   System.Reflection.BindingFlags.Instance |
   System.Reflection.BindingFlags.Public |
   System.Reflection.BindingFlags.SetProperty,
   null, this, new object[] { "Reflected!" });

// Change form's private dialogResult field
myType.InvokeMember("dialogResult",
   System.Reflection.BindingFlags.FlattenHierarchy |
   System.Reflection.BindingFlags.Instance |
   System.Reflection.BindingFlags.NonPublic |
   System.Reflection.BindingFlags.SetField,
   null, this, new object[] { DialogResult.OK });

// Hide the form
myType.InvokeMember("Hide",
   System.Reflection.BindingFlags.FlattenHierarchy |
   System.Reflection.BindingFlags.Instance |
   System.Reflection.BindingFlags.Public |
   System.Reflection.BindingFlags.InvokeMethod,
   null, this, new object[0]);

 

// Using Reflector
Reflector Me = new Reflector(this);

// Change form's text
Me.SetProperty("Text", "Reflected!");

// Change form's private dialogResult field
Me.PrivateAccess = true;
Me.SetField("dialogResult", DialogResult.OK);

// Hide the form
Me.InvokeMethod("Hide", Me.NoArgs);

Not all overloads of all methods have been tested thouroughly. Feel free to post questions, comments and bugs.

reflector.zip

Edited by AWS
[sIGPIC]e[/sIGPIC]
  • 3 weeks later...
  • Leaders
Posted
Fixed a big in Reflector.cs and then converted the class to VB. Note that I did very little testing on the VB version and it is very possible that a bug or two or ten were introduced in the translation.

Reflector.zip

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...