Implementing a Plug-in Loader in C# (Using Reflection)


I always asked myself how the Plugins work. So I read some articles about it, and found some really interesting ideas. I’ve found the following way for implementing plugins, it does not mean that it’s the only one, but you can research more if this approach does not work for you.

When do I need to implement a plugin?
I can think in some scenarios such as:

  • When the business rules change often and you don’t want to recompile the whole application for an udpate. (You can solve this by developing with layers, and others techniques).
  • In order to give the chance to thrid party for implementing functionally in your application.

So, What do I need for a plugin?
Easy, you need 3 key parts:

  1. Contract definition: This part of you application is in charge of expose all the tasks that the plugin has to implement, and also exposes the application’s resources that the plug in allowed to access.
  2. Contract implementation: The final implementation of tasks.
  3. Plugin loader: This part of you application is going to load all the files related to the application. The final result is to have a way to access all the loaded plugins and its members.

Sounds easy right? In fact, it’s easy, the only thing is how you could implement each step in you favorite language.

I have implemented a simple loader by using C# 3.0, LINQ and Generics. I based my example on a article that I found in Internet, however I added the Generics and LINQ in order to take advantage of .NET 3.5.

Step 1: Plugin contract.
In C# the most common way for defining a contract is by using a Interface. So, we only need to create a project that is going to store all the interfaces that plug in has to implement, and the resources that are shared among the plug in and the application.

Step 2: Plugin Implementation
Now, we have to create a class that implements (which means write the functionality) for the contracts of the previous step.

Step 3: Plugin loaders
Here is the tricky part of this, our application is going to load binary files. Those files are going to be incorpored to our running application. After that, we should be able to call (if required) a particular task from a plugin. In order to do this in C#, we use ‘late binding’ concept, that .NET includes. It allows our application for loading assemblies in runtime. Also by using reflextion we can generate instances of our loaded assemblies.

Here is a screenshot of my sample in C#:


So, I’m attaching the related code as an example of how you could implement a plug in loader.
https://github.com/hmadrigal/playground-dotnet/tree/master/MsDotNet.PluginWithReflection
http://hmadrigal.googlepages.com/Transtool.zip

Also you can check some really nice articles:

Creating a Flexible Dynamic Plugin Architecture under .NET
http://www.codeproject.com/KB/dotnet/PluginManagerClassBrk.aspx?fid=26061&df=90&mpp=25&noise=3&sort=Position&view=Quick

ASP.NET Technical Articles – Creating a Plug-In Framework
http://msdn.microsoft.com/en-us/library/ms972962.aspx

Dynamic Loading Made Simple
http://www.codeproject.com/KB/library/dynamicloading.aspx

Enabling Your Application to Become a Plugin Host
http://www.codeproject.com/KB/cs/Plugin_Basics.aspx

Let Users Add Functionality to Your .NET Applications with Macros and Plug-Ins
http://msdn.microsoft.com/en-us/magazine/cc164072.aspx

Plugin Architecture using C#
http://www.codeproject.com/KB/cs/c__plugin_architecture.aspx

Regards,
Herberth

Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s