Click here to Skip to main content
14,035,739 members
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 15 Feb 2013
Licenced CPOL

How to create an easy plugin-system in C#

, 15 Feb 2013
Rate this:
Please Sign up or sign in to vote.
How to build an easy plugin system with C# which only takes about 30 lines of code!


This article will describe how to implement a simple Plugin-system into your C#-Application which can be very useful for several situations your tool has to implement different behavior. This code snippet will only make use of standard .NET classes which are built-in the Microsoft Framework! No 3rd party tools are needed!


Since I started developing C#-Desktop-Applications about 4 years ago I really ran across several problems. One was, to implement a stable plug-in system with which you could easily spread code and functionality. One big problem was, that no one nowhere had a good tutorial on how to build this. So I decided to write my own, first tutorial in English. So please be gentle with my grammar.

Using the code

The .NET Framework offers two possibilities to load assemblies to the current AppDomain. The following code uses the same functionality Visual Studio has when you're adding a new reference to your project. The code to include an assembly uses the System.Reflection namespace and looks as following:

//This code loads a new dll to the current AppDomain.

Now, the assembly is loaded into your application but you can't really use anything in it because normally you don't really know what functionality the plug-in offers. Also, it's pretty easy to access a DLL in a normal executable but the way back can be very annoying.

Now, to access the DLL, we need its assembly name which should be equal to the main-namespace of it. Unfortunately, there is no way to get the default namespace of a dynamically added assembly in C#. One possibility is now to always expect the plug-in to have the same namespace as the application we're loading it in. But that doesn't matter at the moment. Once we've loaded our plug-in, we start using it via Reflection, which goes as follows.

//Loop through all opened assemblies in the current AppDomain
foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
    foreach (Type t in a.GetTypes())
        if (t.GetInterface("MyFunctionInterface") != null)
                MyFunctionInterface pluginclass = Activator.CreateInstance(t) as MyFunctionInterface;

Easy, right? Working with plug-ins is actually no problem if you understand how easy you can write dynamic code.

Points of Interest

There is also a possibility in the System.Reflection to load only the bytes of a DLL. By now, I haven't really had a chance of using it because I can't really imagine a scenario where this would be more useful than linking to the location of the file.


  • Version Initial with all the information and a working .NET Solution to show how plug-ins can work!


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


About the Author

Software Developer (Junior)
Switzerland Switzerland
I started my apprenticeship as a computer scientist in 2009 and now I'm currently working on my degree. I love music and the combination between IT and music. That's why I have a studio where i produce music.

You may also be interested in...


Comments and Discussions

SuggestionNice Project Pin
Member 1403468826-Oct-18 23:53
memberMember 1403468826-Oct-18 23:53 
QuestionHow to create an easy plugin-system in C# Pin
Member 802512715-May-18 9:51
memberMember 802512715-May-18 9:51 
QuestionDon't Run For Me... Pin
Menci Lucio13-Oct-16 0:05
professionalMenci Lucio13-Oct-16 0:05 
AnswerRe: Don't Run For Me... Pin
Menci Lucio14-Oct-16 0:53
professionalMenci Lucio14-Oct-16 0:53 
QuestionGreat Pin
Ahmed Bahaggag3-Nov-14 21:33
memberAhmed Bahaggag3-Nov-14 21:33 
QuestionGreat Tip Pin
Jorge Gomez Prada28-Mar-14 5:39
professionalJorge Gomez Prada28-Mar-14 5:39 
QuestionOptimization Pin
klinkenbecker5-Jan-14 12:28
memberklinkenbecker5-Jan-14 12:28 
QuestionNice Pin
klinkenbecker5-Jan-14 9:28
memberklinkenbecker5-Jan-14 9:28 
QuestionDoesnt Work. Pin
EveryNameIsTakenEvenThisOne15-Nov-13 18:06
professionalEveryNameIsTakenEvenThisOne15-Nov-13 18:06 
AnswerRe: Doesnt Work. Pin
Dread_Sharp21-Nov-13 5:13
memberDread_Sharp21-Nov-13 5:13 
AnswerRe: Doesnt Work. Pin
klinkenbecker5-Jan-14 9:27
memberklinkenbecker5-Jan-14 9:27 
Questionan improvement Pin
leizhiyuan16-Feb-13 18:32
memberleizhiyuan16-Feb-13 18:32 
AnswerRe: an improvement Pin
Dread_Sharp21-Feb-13 20:09
memberDread_Sharp21-Feb-13 20:09 
GeneralRe: an improvement Pin
leizhiyuan21-Feb-13 20:10
memberleizhiyuan21-Feb-13 20:10 
GeneralMy vote of 5 Pin
revive13915-Feb-13 4:20
memberrevive13915-Feb-13 4:20 
GeneralMEF! Pin
db7uk15-Feb-13 3:23
memberdb7uk15-Feb-13 3:23 
GeneralRe: MEF! Pin
Dread_Sharp15-Feb-13 3:29
memberDread_Sharp15-Feb-13 3:29 
GeneralRe: MEF! Pin
db7uk15-Feb-13 3:34
memberdb7uk15-Feb-13 3:34 
QuestionIdea to extend and some optimizations Pin
Reinhard Ostermeier15-Feb-13 2:51
memberReinhard Ostermeier15-Feb-13 2:51 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190424.1 | Last Updated 15 Feb 2013
Article Copyright 2013 by Dread_Sharp
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid