Plugin System with C#

  • Thread starter Thread starter Rudolf
  • Start date Start date
R

Rudolf

Dear NG,

I want to create a Plugin System in C# (WinForms). Has anybody experience
about this, tips, tricks, or any links. Thank you very much

The DotNetJunkie
 
Hello,

You will need to use Reflection
You should also consider using interfaces

Assemblies:
1) MyApp.exe - the entrypoint and main program
2) PluginHelper.dll - Defines the plugin interface(s). Provides routines to
dynamically load an assembly and return an object that implements a plugin
interface.
3) MyPlugin1.dll, MyPlugin2.dll, etc - implements the plugin interface

All assemblies reference PluginHelper.dll so keep this assembly small and
keep changes to a minimum.

<code assembly="MyApp.exe">
// use static loader when you need to get multiple instances from the same
assembly
PluginHelper helper=PluginHelper.LoadPlugin("MyPlugin1.dll");
IPlugin plugin=(IPlugin) helper.CreateInstance("IPlugin");
plugin.DoSomething();

...or...

// use static instantiator when you only need one instance from an assembly
IPlugin plugin=(IPlugin)
PluginHelper.CreateInstance("MyPlugin1.dll","IPlugin");
plugin.DoSomething();
</code>

Every interface you need will have to be in PluginHelper.dll.
Define these first!
How you implement PluginHelper is up to you.

You might want it to search the current subdirectory instead of supplying an
assembly name.
You may want to try and use attributes to mark assemblies as plugin
assemblies to save you some time searching.
You may want to try and use attributes to mark classes as plugin classes for
the same reason
You might want to look at caching the assemblies/classes for faster future
searches.
You might want to put a factory class inside each assembly and use the
factory to instantiate the objects inside it instead.
This is great when an assembly implements many classes and it avoids
ambiguity as to which class to return when searching for an "entrypoint"
instance to return in an assembly. This way, an assembly can implement many
plugin interfaces but still be treated as one object.

<code>
// static loader looks for and returns an IPluginFactory object
IPluginFactory factory=PluginHelper.Load("MyPlugin2.dll")
factory.CreateClass1();
factory.CreateClass2();
factory.Find("car");
</code>

This can get as complex as you like but the basics are there

Good luck...
Oscar Papel
 
Back
Top