Active Directory Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail
How Long For Me
<< Back To All Blogs
Generic Method for Loading Interfaces in C# (For a Plugin System)
Saturday, May 30th, 2009
I had some issues with loading a plugin system that I've been working on but it turned out to be not my bad coding but my bad habits: I was improperly including a file which was confusing the activator.
Now that I've sorted out my issues, I figured I would save some of you out there some time by providing a C# Generic method to load interfaces for a plugin system.
I am aware that there is a System.AddIn method in C# 3.5, but after looking at it and starting to play with it my code looked so bloated that I decided to scratch it, and go back to the old and "real" way of doing a plugin framework in C#.
Without further ado, I present to you my Utility class with (for the time being) a single method that will read a directory, find all DLLs and return a list of interfaces of the specified type:
public static class Utilities<T>
public static List<T> LoadPlugins(string Path)
List<T> toReturn = new List<T>();
string files = Directory.GetFiles(Path, "*.dll");
foreach (string file in files)
Assembly a = Assembly.LoadFrom(file);
Type types = a.GetTypes();
foreach (Type t in types)
T ipd = (T)Activator.CreateInstance(t);
You can then invoke this method as follows:
List<IMyInterface> plugins = Utilities<IMyInterface>.LoadPlugins(PluginPath);
Obviously some error checking should be present in this as well, but I kept that out for the sake of simplicity.
Pretty nifty eh?
Interfacin' Tom Out.
Using MOSS and WSS SharePoint Workflow to Resize Images in an Image Library
Reading A Database Schema in SQLite with C#
Reading an XML file using LINQ
Querying Table Entities with Microsoft Azure (And ADO.NET Data Services Framework)
Updating an LDAP Property in C#
Petar Petrov said on Wednesday, June 3rd, 2009 @ 12:49 AM
I don't like static classes neither returning a List. Please this post of Eric Lippert (http://blogs.msdn.com/ericlippert/archive/2008/09/22/arrays-considered-somewhat-harmful.aspx). Static classes can't be tested easily.
I would write something like this instead
public class PluginLoader
public IEnumerable Load(string path)
return Load(path, "*.dll");
public IEnumerable Load(string path, string searchPattern)
var files = Directory.GetFiles(path, searchPattern);
foreach (var file in files)
var assembly = Assembly.LoadFrom(file);
var types = assembly.GetTypes();
foreach (var type in types)
yield return (T)Activator.CreateInstance(type);
It's still your code with some changes :)
Tom said on Saturday, May 30th, 2009 @ 5:58 PM
Thanks @danlash, they were actually there, I just didn't escape them properly in the post. Looks like it's time for me to update my blog software to do that automatically.
Dan Lash said on Saturday, May 30th, 2009 @ 2:01 PM
Looks like you left out the actual generic part of the class or method. T isn't defined anywhere.
Add A Comment
Email Address: (not public, used to send notifications on further comments)
Enter the text above, except for the 1st and last character: