EditAnything You Can Do...
Reflector doesn't do anything that you can't do without it. It's intended to simplify the use of Reflection by providing a standard API. I wrote the Reflector class in early 2003 when I was learning about Reflection in the .Net 1.1 framework. The most obvious functionality it provides is reading/writing values to properties and fields at run-time but it does much more than that.
Important
Please keep in mind: there is some performance overhead for Reflection. It's not as extreme as some people make it out to be, but you should use it sparingly and instead look for ways to leverage new 3.5 language features to accomplish the same goals.
EditExample Class
All examples will use the following class. The class isn't meant for anything other than giving us a concrete reference so that the examples should be a little clearer.
// Hi, I'm a class that doesn't compile or do anything
public class Simple
{
internal string _string1;
internal DateTime _dateTime;
protected int _integer;
protected decimal _decimal;
private float _float;
private long _long;
private bool _bool;
private double _double;
public bool Boolean
{
get { return _bool; }
set { _bool = value; }
}
public double Double
{
get { return _double; }
set { _double = value; }
}
internal void InternalGenericMethod<T>(T instance)
{
}
protected static void ProtectedMethod(string argument)
{
}
private string PrivateMethod(string argument1, string argument2)
{
}
public T GenericMethod<T>()
{
return default(T);
}
}
EditAccess Level
Reflector can access private, protected and even internal members and methods. This is controlled by setting the PublicOnly member to true or false. Setting it to true will limit Reflector to only public only members and methods, false will allow it to access
everything in the type. The default value is true.
//Limits access to public members only
Reflector.PublicOnly = true;
//Allows access to everything
Reflector.PublicOnly = false;
Important
Because Reflector is a static class, you need to be aware that changing the value of the PublicOnly property will affect all calls made to Reflector.
EditInheritance
Reflector can also access members and methods from inherited classes by setting SearchHierarchy to true. In conjunction with PublicOnly set to false, Reflector will access private and internal members and methods. This is important to keep in mind if you want to target a specific override of a member or method. The default value is false.
//Limits Reflector to only inspecting the immediate type
Reflector.SearchHierarchy = false;
//Will cause Reflector to search the entire hierarchy
Reflector.SearchHierarchy = true;
EditCalling Methods
There are 4 CallMethod overloads in the Reflector class for the dynamic invocation of methods. The reason for so many versions is to support generic parameters as well as static and instance methods. There are 2 overloads for instance methods and 2 overloads for static methods (one generic, one non-generic in each case). There are a few important things to note about how Reflector determines which method overload to call. Primarily, when arguments are being passed, Reflector projects the argument types so that it can determine the most appropriate overload to call. This does work for methods which take generic parameters which affect the type of arguments supplied.
Using the (lame) example class above, here are some examples to call each of the methods:
Reflector.PublicOnly = false;
Reflector.SearchHierarchy = true;
var instance = new Simple();
var simpleType = typeof(Simple);
var person = new Person();
var personType = typeof(Person);
// Call the internal method with a generic parameter of Person
// First argument - the instance which owns the method we're calling
// Second argument - the name of the method to call
// Third argument - the arguments, in order, to pass to the method
// Fourth argument - when supplied, this type array populates the generic arguments to the generic method
Reflector.CallMethod(instance, "InternalGenericMethod", new object[] { person }, personType);
// Call the protected static method on the type
// First argument - the type to call the static method on
// A parameter array will work for non-generic method calls
Reflector.CallMethod(simpleType, "ProtectedMethod", "a string");
// Call the private method on the instance
Reflector.CallMethod(instance, "PrivateMethod", "string 1", "string 2");
// Call the public generic method
Reflector.CallMethod(instance, "GenericMethod", new object[]{}, personType);
EditAccessing and Writing Members (Fields & Properties
Reflector makes things very simple for you when reading and writing static or instance members by handling type conversion and traversing the hierarchy and including private, protected and internal levels just as with method calls.
Reflector.PublicOnly = false;
Reflector.SearchHierarchy = true;
var instance = new Simple();
Reflector.Read(instance, "_string1"); // returns the value of the _string1 member
Reflector.Write(instance, "_string1", "hello world"); // writes 'hello world' to _string1
Reflector.Write(instance, "_dateTime", "07/01/2005"); // Reflector handles the conversion
Reflector.Write(instance, "Boolean", 1); // Notice, there is no difference between writing to a property or field
Reflector.Write(instance, "_bool", "true"); // This works too