logo
  • Jobs
  • About Me
  • Contact
  • Home
« A WinForms HyperLink control
A WTF moment »

A flexible plugin loader

Posted July 11th, 2005 by Matt Berther

Ever added plugin capabilities to your application? Are you tired of writing the same discovery and loading code over and over? I am too, and came up with this fun little class:

class PluginLoader
{
    public static ConstructorInfo[] FindPlugins(Type pluginType)
    {
        ArrayList list = new ArrayList();
 
        foreach (string file in Directory.GetFiles(Environment.CurrentDirectory, "*.dll"))
        {
            try
            {
                FileInfo fileInfo = new FileInfo(file);
                string assemblyPath = fileInfo.Name.Replace(fileInfo.Extension, "");
                Assembly asm = AppDomain.CurrentDomain.Load(assemblyPath);
 
                foreach (Type t in asm.GetExportedTypes())
                {
                    if (pluginType.IsAssignableFrom(t))
                    {
                        ConstructorInfo ctor = t.GetConstructor(Type.EmptyTypes);
                        if (ctor != null)
                        {
                            list.Add(ctor);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine("Exception encountered loading plugin: " + e.ToString());
                // this is deliberately ignored, as any error in loading the assembly should just involve
                // continuing on                    
            }
        }
 
        return (ConstructorInfo[])list.ToArray(typeof(ConstructorInfo));
    }
}

Wherever you want to get a list of available plugins, use PluginLoader.FindPlugins(typeof(YourPluginType));. The method returns an array of ConstructorInfo objects so you will be responsible for instantiating the objects yourself. This is the heavy part of the reflection though, so its only done once and you can instantiate when you need the plugin.

One thing I would probably like to do is to get the discovery mechanism happening in a secondary appdomain. This will help by keeping assemblies unloaded until they are actually used by the application.

3 Comments

This entry was posted on Monday, July 11th, 2005 at 2:34 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leif Wickland
July 13th, 2005

I recommend the plugin approach described by this article (http://msdn.microsoft.com/msdnmag/issues/05/07/Reflection/) because it allows an application to start much more quickly. Unfortunately, it’s not as dynamic as the method Matt provides.

Matt Berther
July 13th, 2005

Leif: The attribute model is nice too, but if you wanted to best of both worlds (ie: flexibility and startup speed), you could easily queue a worker thread to run the PluginLoader.

Keith
December 12th, 2006

What I want to do on my blog, is every few hours take the oldest post and move it to the
front of the queue, all automatically. Anyone know if there is a plugin that can do this or
a simple way to set up another plugin to do this (use my own feed perhaps)?
Thanks.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
-->

flag
Favorite Charity
wounded warrior project
Search
Social
  • mattberther on twitter
  • mattberther on linkedin
Syndication
Archives
  • January 2010
  • September 2009
  • July 2009
  • June 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • September 2008
  • August 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
  • July 2004
  • June 2004
  • May 2004
  • April 2004
  • March 2004
  • February 2004
  • January 2004
  • December 2003
  • November 2003
  • October 2003
  • September 2003
  • August 2003
  • July 2003
  • June 2003
  • May 2003
  • April 2003
  • March 2003
mattberther.com © 2003 - 2010