Click here to Skip to main content
Licence CPOL
First Posted 26 Jun 2005
Views 47,758
Bookmarked 41 times

Plugin-Ready Application Development

By | 26 Jun 2005 | Article
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:

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:)

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:

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)

About the Author

Dario Solera

Founder
Threeplicate
Italy Italy

Member

Follow on Twitter Follow on Twitter
Google+
Dario Solera is the co-founder of Threeplicate Srl.
 
He is also the founder and main developer of the ScrewTurn Wiki project, now maintained by Threeplicate.
 
His interests are mainly politics, F1 and digital photography. He hopes, someday, to learn skydiving.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberoraomk1:30 1 Feb '12  
GeneralWrong Interface PinmemberXIUnin23:48 7 Jan '06  
GeneralRe: Wrong Interface PinmemberDario Solera6:37 8 Jan '06  
GeneralSpecified cast is not valid PinsussAnonymous23:02 8 Aug '05  
GeneralRe: Specified cast is not valid Pinmembergpmaker11:16 18 Jul '06  
AnswerRe: Specified cast is not valid Pinmembernetdel21:54 23 Oct '06  
GeneralUnLoad PlugIns PinmemberFredyAlfredo11:00 26 Jun '05  
GeneralRe: UnLoad PlugIns Pinmemberdarisole21:02 26 Jun '05  
GeneralRe: UnLoad PlugIns PinmemberThomas Lykke Petersen21:09 26 Jun '05  
GeneralRe: UnLoad PlugIns Pinmemberdarisole21:16 26 Jun '05  
GeneralRe: UnLoad PlugIns PinmemberThomas Lykke Petersen2:15 27 Jun '05  
GeneralRe: UnLoad PlugIns Pinmembermav.northwind23:15 26 Jun '05  
GeneralRe: UnLoad PlugIns PinsussAnonymous4:06 18 Aug '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 26 Jun 2005
Article Copyright 2005 by Dario Solera
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid