Leaders snarfblam Posted November 8, 2006 Leaders Posted November 8, 2006 (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 October 15, 2021 by AWS Quote [sIGPIC]e[/sIGPIC]
Leaders snarfblam Posted November 12, 2006 Author Leaders Posted November 12, 2006 Updated Reflector class to climb inheritance hierarchy to find private members of base classes.Reflector.zip Quote [sIGPIC]e[/sIGPIC]
Leaders snarfblam Posted December 2, 2006 Author Leaders Posted December 2, 2006 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 Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.