Mental Jetsam

By Peter Finch

Dynamically creating C# class instances using reflection.

Posted by pcfinch on November 27, 2008

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.

  1. 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() ;
    }

  2. 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
      }
    }

  3. 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.

Advertisements

3 Responses to “Dynamically creating C# class instances using reflection.”

  1. Fei. said

    you are creating objects, not classes

  2. Graeme said

    This code does not dynamically create classes, it creates objects or instances of classes. Dynamically creating classes means that you are actually defining new types at runtime.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: