Home
IPhone SMS To Gmail
Consulting
Blog
Software
Projects
Links
About
Contact
Blog
Facebook
Twitter
LinkedIn
|
<< 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)
{
if (t.GetInterfaces().Contains<Type>(typeof(T)))
{
T ipd = (T)Activator.CreateInstance(t);
toReturn.Add(ipd);
}
}
}
return toReturn;
}
}
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.
Tags
CSharp
Related Blogs
Resizing Images in C#
Retrieving the SID of a user or group account using the Win32 SDK and C#
Determining if a computer is a laptop or desktop in C#
Bitmasking userAccountControl attribute in LDAP from C#
Calculating ISO 8601 Date formats in C#, C++, and Java
Comments
Petar Petrov said on Wednesday, June 3rd, 2009 @ 12:49 AM
Hi.
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)
{
if (type.GetInterfaces().Contains(typeof(T)))
{
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
Name:
URL:
Email Address: (not public, used to send notifications on further comments)
Comments:
Please enter the text from the image:

|