Reflection, the ability for a program to dynamically inspect itself, is possibly one of the most powerful features of modern programming languages like C# and Java. The following is a simple example of how to interrogate the current C# assembly, locate all the classes that are derived from a base class, and then dynamically created and executed an instance of the class.
I wrote this code for a set of tests I wanted to implement. The idea was to make it simple to add new tests to the project by just adding new class derived from the base “Test” class. The program would then automatically find all the tests and run them.
- First, create the “Test” base class.
abstract class Test { protected String m_sName ; public String name { get { return (m_sName) ; } } protected Test(String sName) { m_sName = sName; } abstract public void run() ; }
- Derive the actual tests from the “Test” base class.
class UsersTest : Test { public UsersTest() : base("Test user names and passwords") { } override public void run() { // Test code does here } }
- To run the tests, first get the current Assembly, and find all the Types that are “Classes” and are derived from the class “Test”. Once you have the type you need to Dynamically create and instance of it. This method looks for the default constructor (the one with no parameters) and uses that, however, the code could look for any constructor if required. It then calls the contractor to
create an instance of the class, and finally executes the classes run() method.
Assembly asm = Assembly.GetExecutingAssembly(); foreach (Type type in asm.GetTypes()) { if (type.IsSubclassOf(typeof(Test)) && type.IsClass) { ConstructorInfo ci = type.GetConstructor(new Type[] { }); Test t = (Test)ci.Invoke(new Object[] { }) ; Console.Out.WriteLine ("Running Test - " + t.name); t.run(); } }
This is a very simple example, and there are other ways to runs tests, but it is easy to see how this design could be extended and used in all sorts of situations.