Click here to Skip to main content
11,932,103 members (59,365 online)
Click here to Skip to main content
Add your own
alternative version


255 bookmarked

Plugin Architecture using C#

, 3 Aug 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
How to make plugins to work with .NET

Sample Image - Plugins.jpg


This article demonstrates to you how to incorporate a single module, as a plugin for another application or use it as a standalone application. The article will demonstrate how a minimal change is required to obtain the above result.


The basic idea for this article has been adopted from the net, I don't remember the original author, but I found the code as a console application, while what I was searching was a method to use one of my windows application module in another application and run it standalone as well with as minimal changes a possible. A little changes to the console application, and this article is what the final result looks like.

Using the code

The key to a plugin architecture is the implementation of a minimal set of methods by the plugins. These methods are used by the main (incorporating) application to find and recognize the plugins. This minimal set of method is achieved by making an interface, which would declare the methods, that are to be implemented by the plugins. This interface has been defined as follows:

public interface IPlugin
    string Name{get;set;}
    IPluginHost Host{get;set;}
    void Show();

The interface define a few methods. Name is used to get the name of the plugin to be shown on the main application in the menu. IPluginHost is used to let the plugin know who is hosting the plugin. Show will show the main form of the plugin application.

We now make our first plugin in form of a windows application like this.

using System;
using PlugIn;

namespace Dynamic
    class PlugIn : IPlugin
        private string m_strName;
        private IPluginHost m_Host;
        public PlugIn()
            m_strName = "Dynamic"; 
        public string Name
            get{return m_strName;}
        public void Show()
            Main1 mn = new Main1();

        public IPluginHost Host
            get{return m_Host;}

public class Main1 : System.Windows.Forms.Form 
    //Your original code goes here...

    static void Main() 
        Application.Run(new Main1());

The above code declare the a plugin class so that the parent application could recognize it as a plugin. The application also declare a form class, which is the startup form.

Now compile the plugin as an executable file. Run it, it should run as a normal standalone exe. Copy the exe into the main applications executable directory, and rename it as a DLL instead of a exe.

Now we move to our main application:

for that we have to interface, which is implemented by our application so that it registers with the plugins.

public interface IPluginHost
    bool Register(IPlugin ipi);

The main application is implemented as windows form.

public class Form1 : System.Windows.Forms.Form, IPluginHost 
    private System.Windows.Forms.MainMenu mainMenu1;
    private System.Windows.Forms.MenuItem menuItem1; private IPlugin[] ipi;

    private void Form1_Load(object sender, System.EventArgs e)
        string path = Application.StartupPath;
        string[] pluginFiles = Directory.GetFiles(path, "*.DLL");
        ipi = new IPlugin[pluginFiles.Length];

        for(int i= 0; i<pluginFiles.Length; i++)
            string args = pluginFiles[i].Substring(

            Type ObjType = null;
                // load it
                Assembly ass = null;
                ass = Assembly.Load(args);
                if (ass != null)
                    ObjType = ass.GetType(args+".PlugIn");
            catch (Exception ex)
                // OK Lets create the object as we have the Report Type
                if (ObjType != null)
                    ipi[i] = (IPlugin)Activator.CreateInstance(ObjType);
                    ipi[i].Host = this;
            catch (Exception ex)


In OnLoad(), we check for all the DLLs present in the application directory, and scan if any implement the plugin interface. If it does we store it and register the plugin with us. This plugin in turn calls the register method on the main application. The register method add the plugin name with out menu.

public bool Register(IPlugin ipi)
    MenuItem mn = new MenuItem(ipi.Name,new EventHandler(NewLoad));

    Console.WriteLine("Registered: " + ipi.Name);
    return true;

The next time the plugin menu is click, we check for the respective plugin name and call the appropriate plugin.

private void NewLoad(object sender, System.EventArgs e) {
    MenuItem mn = (MenuItem)sender; for(int

    i=0; i < ipi.Length; i++)
        string strType = mn.Text;

Now compile and run the application, with the plugin DLL in the application path, the plugin name would show in the menu. Click the menu and the plugin pops up. Hope you find the code useful.


  • This is the initial release 1.0.0R.


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


About the Author

Team Leader
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Sergey Alexandrovich Kryukov23-Jul-14 15:55
mvpSergey Alexandrovich Kryukov23-Jul-14 15:55 
GeneralMy vote of 1 Pin
Sergey Alexandrovich Kryukov23-Jul-14 15:47
mvpSergey Alexandrovich Kryukov23-Jul-14 15:47 
GeneralMy vote of 5 Pin
mike_dcsku25-Sep-12 19:37
membermike_dcsku25-Sep-12 19:37 
GeneralWhy use IPluginHost Pin
nulkoud16-Jun-09 2:41
membernulkoud16-Jun-09 2:41 
GeneralRe: Why use IPluginHost Pin
sameh_serag15-Oct-09 3:44
membersameh_serag15-Oct-09 3:44 
GeneralCast Pin
assismvla19-Apr-09 11:13
memberassismvla19-Apr-09 11:13 
GeneralRe: Cast Pin
Manadar14-May-09 4:25
memberManadar14-May-09 4:25 
GeneralConvert to Pin
Michalss28-Mar-09 23:49
memberMichalss28-Mar-09 23:49 
GeneralProof read proof read proof read!!!!! Pin
havyck5-Apr-07 11:08
memberhavyck5-Apr-07 11:08 
GeneralRe: Proof read proof read proof read!!!!! Pin
justapgmr14-Jan-09 6:23
memberjustapgmr14-Jan-09 6:23 
GeneralRe: Proof read proof read proof read!!!!! Pin
wibblewoo28-Jan-09 2:22
memberwibblewoo28-Jan-09 2:22 
GeneralRe: Proof read proof read proof read!!!!! Pin
jt_2312-Jul-09 6:04
memberjt_2312-Jul-09 6:04 
GeneralA little bit code optimization Pin
Alexander M. Batishchev11-Apr-06 1:13
memberAlexander M. Batishchev11-Apr-06 1:13 
GeneralRe: A little bit code optimization Pin
Alexander M. Batishchev11-Apr-06 13:47
memberAlexander M. Batishchev11-Apr-06 13:47 
GeneralRe: A little bit code optimization Pin
David30019-Dec-06 8:48
memberDavid30019-Dec-06 8:48 
GeneralRe: A little bit code optimization Pin
Alexander M. Batishchev3-Apr-07 5:21
memberAlexander M. Batishchev3-Apr-07 5:21 
GeneralRe: A little bit code optimization Pin
Henk Burgstra4-Aug-07 16:21
memberHenk Burgstra4-Aug-07 16:21 
GeneralRe: A little bit code optimization Pin
BZZR14-Nov-07 20:49
memberBZZR14-Nov-07 20:49 
GeneralRe: A little bit code optimization Pin
Member 20530069-Sep-09 1:30
memberMember 20530069-Sep-09 1:30 
Questionother directory than app path?? Pin
Badscher15-Feb-06 0:42
memberBadscher15-Feb-06 0:42 
AnswerRe: other directory than app path?? Pin
Alexander M. Batishchev12-Apr-06 2:15
memberAlexander M. Batishchev12-Apr-06 2:15 
GeneralReflection in VB2005 Pin
hrgy846-Jan-06 8:53
memberhrgy846-Jan-06 8:53 
GeneralSecurity concerns Pin
Biju P K17-May-05 1:13
memberBiju P K17-May-05 1:13 
GeneralRe: Security concerns Pin
Valer BOCAN26-Sep-05 3:44
memberValer BOCAN26-Sep-05 3:44 
GeneralRe: Security concerns Pin
MrOzark16-Oct-05 20:07
memberMrOzark16-Oct-05 20:07 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 4 Aug 2003
Article Copyright 2003 by shokisingh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid