Click here to Skip to main content
15,860,859 members
Articles / Programming Languages / C#
Article

Plugin-Ready Application Development

Rate me:
Please Sign up or sign in to vote.
2.74/5 (18 votes)
26 Jun 2005CPOL1 min read 67.7K   47   13
How to design an app that can easily use plugins.

Introduction

Days ago I was thinking about creating some plugins for an application that I had developed. How to do that? The idea: distribute plugins as .NET DLLs that expose specific interfaces.

Architecture

Each plugin must have at least one main method attending to a specific duty. In a simple plugin system a single method is enough to draw the architecture.

Design of the interface

Supposing the plugin computes a value from a parameter, the plugin’s interface is:

C#
interface IPlugin {
   static int MainMethod(object o);
}

All the plugins must implement this interface.

Linking to the plugin

Now it is necessary to explain how to use the plugin at runtime. The plugins are saved in the ‘\Plugins’ directory inside the startup path of the application. The code for the usage of plugins is very simple: (Please note the using clause:)

C#
using System.Reflection;

private string pluginDir = Application.StartupPath + "\\Plugins";

private IPlugin[] plugins;

private void loadPlugins() {

   string[] p = Directory.GetFiles(pluginDir);
   // Assuming there are no files different that plugins’ DLLs
   plugins = new IPlugin[p.Length];
    
   for(int i = 0; i < 0; i++) {
      Assembly asm = Assembly.LoadFrom(p[i]);
      // Must be a full-qualified name (MyNamespace.IPlugin)
      Type type = asm.GetType("IPlugin", true);
      plugins[i] = (IPlugin)Activator.CreateInstance(type);
   }
}

Using plugins

After invoking loadPlugins(), all the plugins can be used as follows:

C#
myResult = plugins[myIndex].MainMethod(myParam);

Now, it is enough to smartly manage the myIndex value…

Improvements

This technique is very 'general purpose'. For your application you may define different interfaces to implement many useful functions. You can use a main interface that tells the plugin-user app which type of plugin to expect and invoke the correct methods. Another ‘support’ interface may expose the menu entries and toolbar buttons.

Conclusions

This article is an abbreviation (and a generalization) of a real project that is currently working. Once you know how to design the architecture, you can do your job without much problems, but it is not always very easy, particularly while developing certain types of plugin, where you have to modify the interfaces and update all the old plugins…

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Italy Italy
Software Development Manager working on IaaS cloud computing. Cloud believer, (former) entrepreneur, F1 addict.

Follow me at dariosolera.it or on Twitter.

Comments and Discussions

 
GeneralMy vote of 1 Pin
oraomk1-Feb-12 1:30
oraomk1-Feb-12 1:30 
GeneralWrong Interface Pin
Steve Hansen7-Jan-06 23:48
Steve Hansen7-Jan-06 23:48 
GeneralRe: Wrong Interface Pin
Dario Solera8-Jan-06 6:37
Dario Solera8-Jan-06 6:37 
GeneralSpecified cast is not valid Pin
Anonymous8-Aug-05 23:02
Anonymous8-Aug-05 23:02 
GeneralRe: Specified cast is not valid Pin
gpmaker18-Jul-06 11:16
gpmaker18-Jul-06 11:16 
AnswerRe: Specified cast is not valid Pin
netdel23-Oct-06 21:54
netdel23-Oct-06 21:54 
GeneralUnLoad PlugIns Pin
FredyAlfredo26-Jun-05 11:00
FredyAlfredo26-Jun-05 11:00 
GeneralRe: UnLoad PlugIns Pin
Dario Solera26-Jun-05 21:02
Dario Solera26-Jun-05 21:02 
GeneralRe: UnLoad PlugIns Pin
Thomas Lykke Petersen26-Jun-05 21:09
Thomas Lykke Petersen26-Jun-05 21:09 
GeneralRe: UnLoad PlugIns Pin
Dario Solera26-Jun-05 21:16
Dario Solera26-Jun-05 21:16 
maybe you're right. usually we can 'hope' Finalize do its work. in fact the MSDN library tells that Finalize is inherited from object without any specific modification. personally I haven't much experience with AppDomains, but it can be a valid solution. I only repeat that leaving some (not many...) DLLs in memory is not a big problem. think about Photoshop: it loads all the plugins at startup and it doesn't unload them until shutdown...
GeneralRe: UnLoad PlugIns Pin
Thomas Lykke Petersen27-Jun-05 2:15
Thomas Lykke Petersen27-Jun-05 2:15 
GeneralRe: UnLoad PlugIns Pin
mav.northwind26-Jun-05 23:15
mav.northwind26-Jun-05 23:15 
GeneralRe: UnLoad PlugIns Pin
Anonymous18-Aug-05 4:06
Anonymous18-Aug-05 4:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.