C# – Dynamic Class Loading

I have noticed since moving this blog that a lot of people are trying to access some of my old posts. In particular, people seem very interested in my posts on C# Dynamic Class Loading. As such, I have decided to re-post the old version here for you. Note that this is a copy-and-paste job from my old blog; there are no additions. I hope this is useful for those people trying to find it.

The other day I wrote a small tutorial on getting dynamic class loading working for Java. In that post I mentioned that it could also be done in C#, but that it was more difficult, and from my personal experience, there is less information available about it. So, here is how to do it!

First, as with the Java, we need to create our API (Application Program Interface). However, unlike with Java there is an additional requirement: a ‘module manager’ class needs to be within the API. As such you will need to create these two files…

using System;
using System.Reflection;

namespace com.michaelclarkeblog.dynamic.api {

    public static class ModuleManager {

        public static Module getInstance(String fileName) {

            /* Load in the assembly. */
            Assembly moduleAssembly = Assembly.LoadFile(fileName);

            /* Get the types of classes that are in this assembly. */
            Type[] types = moduleAssembly.GetTypes();

            /* Loop through the types in the assembly until we find
             * a class that implements a Module.
             */
            foreach (Type type in types) {
                if (type.GetInterface("Module") != null) {
                    /* Create a new instance of the 'Module'. */
                    return (Module)Activator.CreateInstance(type);
                }
            }

            return null;

        }

    }

}
using System;

namespace com.michaelclarkeblog.dynamic.api {

    public interface Module {
        String getText();
    }

}

Once you’ve got the two files you can compile them into a library….

api $ mcs -t:library Module.cs ModuleManager.cs
api $ ls -a | grep .dll
Module.dll
api $

Now that we’ve made the API the next thing to write is the application that is going to use it for loading modules. This is a very simple application…

using System;
using com.michaelclarkeblog.dynamic.api;

namespace com.michaelclarkeblog.dynamic.application {

    public class Application {

        public static void Main(String[] args) {

            if (args.Length > 0 ) {
                Module module = ModuleManager.getInstance(args[0]);
                Console.WriteLine(module.getText());
            } else {
                Console.WriteLine("Usage: mono Application.exe module_name");
            }
        }

    }

}

Now we can compile our ‘Application’ against the API…

application $ mcs -reference:../api/Module.dll Application.cs
application $ ls -la | grep exe
-rwxr-xr-x 1 michaelfclarke michaelfclarke 3072 Apr  5 19:32 Application.exe
application $

And now, finally, we can write a couple of modules!

using System;
using com.michaelclarkeblog.dynamic.api;

namespace com.michaelclarkeblog.dynamic.modules {
  public class Mike : Module {
    public String getText() {
      return "Hello from Mike's Module!";
    }
  }
}
using System;
using com.michaelclarkeblog.dynamic.api;

namespace com.michaelclarkeblog.dynamic.modules {
  public class Another : Module {
    public String getText() {
      return "Hello from Another Module!";
    }
  }
}

Compile the modules…

modules $ mcs -t:library -reference:../api/Module.dll Another.cs
modules $ mcs -t:library -reference:../api/Module.dll Mike.cs
modules $ ls -la | grep dll
-rwxr-xr-x 1 michaelfclarke michaelfclarke 3072 Apr  5 19:36 Another.dll
-rwxr-xr-x 1 michaelfclarke michaelfclarke 3072 Apr  5 19:36 Mike.dll
modules $

There is just one last thing we need to do before we can run our application. We need to copy the Module.dll API from the api directory into the application directory…

modules $ cd ../application
application $ cp ../api/Module.dll .

And now lets try running our dynamic application…

application $ mono Application.exe ../modules/Mike.dll
Hello from Mike's Module!

application $ mono Application.exe ../modules/Another.dll
Hello from Another Module!

application $ mono Application.exe
Usage: mono Application.exe module_name

7 thoughts on “C# – Dynamic Class Loading

  1. Jason

    Many thanks for providing this. This is the only example I could find (and understand) to teach myself how to dynamically load dll files.

    Reply
  2. John

    Hey Michael,
    I was wondering, is it possible to dynamically load a class from a DLL in a way where it can
    be inherited? I have been searching far and wide for facts to show whether or not it can be done, and
    to no avail as of yet. Help? >.<

    Reply
  3. Yatooska Simmons

    Is there a way to do this, with more then one module in a dll?
    I’m writing a program that is required to do class loading.
    It is intended that other programmers can add modules.
    My program should be able to load the modules, regardless of either one or more in a single dll.

    Reply
  4. Yatooska Simmons

    My previous comment has been removed, somehow. Anyway, if you read my question, I figured it out. Thanks for this useful example, it did help me get what I needed.

    Reply
  5. Kubi

    Hey, you made your base class as “public interface Module” … is it possible to use “real” class instead of interface? I have problem with doing this and getting error InvalidCastException (Unable to cast object of type derivedClass to type baseClass).

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>