Dynamic Loading is the process of loading code and using it at run time rather than at compile time. Why use it? Simple, dynamic loading makes the process of modifying a program and adding extra functionality later on easier (especially by third-party vendors). Furthermore it makes larger projects far more managable since each section of code functionality is seperate from each other. Many applications use this technique for implementation of plugins, e.g. Visual Studio and CIRIP (see demo link).
The included source shows how to load classes from dll files (dynamical link libraries) at runtime based upon a particular interface.
public static List<object> LoadAssemblies(String path, String interfaceName, bool recursive)
List<object> output = new List<object>();
String files = new String;
files = Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories);
files = Directory.GetFiles(path, "*.dll", SearchOption.TopDirectoryOnly);
foreach (String filename in files)
Console.WriteLine("Loading " + filename);
Assembly assembly = Assembly.LoadFrom(filename);
foreach (Type t in assembly.GetTypes())
if (t.IsClass && !t.IsAbstract && t.GetInterface(interfaceName) != null)
catch (Exception err)
Console.WriteLine("Loading fail:" + err.ToString());
The LoadAssemblies method loads in a single instance of each class implementing interfaceName in dll files found at the path (or in deeper folders if recursive = true is specified).
Using the Code
To use the code the following steps must be taken;
- Create an application where dynamic loading support is required
- Create a new class library (dll) with a copy of the interface all plugins will support
- Create an explit link from the application to the interface library
- Create new class libraries (dlls) containing classes which implement the interface
- Add the included code into the application
- During the load in period of the application call LoadAssemblies; using the path to the dlls & the fully qualified name of the interface (e.g. DNBSoft.InterfaceA).
- Foreach of the returned objects (cast to the interface once returned as the List<object>) call the relevant methods to link the plugin to the application.
Points of Interest
Did you learn anything interesting/fun/annoying while writing the code? Did you do anything particularly clever or wild or zany? No and no, however plugin support is VERY useful, and once you get the hang of it you'll use it alot.
It's the way forward!!!
Version 184.108.40.206 - The one and only release
I originally studied for a masters in engineering of software engineering at The University of Birmingham during 2000-2004, of which I received a 2:1. I continued at Birmingham University working with Civil Engineering and Rail Research UK where I am currently in my final year of a 3 year PhD project developing a Computational Intelligent Approach to Railway Intervention Planning. Although my work has a significant focus on railway engineering and associated practices much of my work is with data mining (on SQL Server 2008) and computational intelligence (CI) techniques. My key areas of expertise in CI are clustering algorithms (including Rival Penalised Competitive Learning) and evolutionary algorithms.
Outside of my formal work I enjoy testing the latest technologies such as .NET 3.5 and the many frameworks of which it comprises (mainly WPF). I have several projects on the go including a .NET and DirectX port of Quake 3 and many utility libraries. I also maintain an extensive website coded in Cold Fusion which is regularly updated; more information is available about me there.