Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share a interface over multiple assemblies

Hello
I have a main app that is going to have some plugins in the ./plugin directory.
Every plugin is a .NET dll and should have the namespace "Plugin" and a class "MainClass" implementing the IPlugin interface defined in the main app.
The problem I have is that I don't know how I could share the same interface across the main app and every plugin without a using reference?

A part of the main app class:

        object oo = Assembly.LoadFile(path).CreateInstance("Plugin.MainClass");
        IPlugin pp = (IPlugin)oo; //Fails if I define the interface in the main app and the plugins

and a part of the first plugin:

namespace Plugin
{
    public class MainClass : IPlugin //I cannot reference the interface in the main app?
    {
       public string getName()
       {
          return "Plugin 1";
       }
    }
}
like image 909
alex Avatar asked Jun 15 '11 14:06

alex


3 Answers

You are correct in that to use an interface (or class, struct etc...) you must reference that assembly. If you do not wish to have your plugins reference the main application (which makes sense) Why not add a new assembly for this purpose? Calling it 'framework' or 'util'.

E.G

Framework Assembly
 - IPlugin

PluginOne Assembly (references Framework)
 - PluginOne : IPlugin

Main App Exe (references PluginOne & Framework assemblies)
like image 173
ColinE Avatar answered Oct 17 '22 01:10

ColinE


You want a class library assembly that contains only the interfaces that both the main application and the plugins are built against.

like image 22
Jesse C. Slicer Avatar answered Oct 17 '22 01:10

Jesse C. Slicer


One point about this:

Every plugin is a .NET dll and should have the namespace "Plugin" and a class "MainClass"

You cannot enforce the plug-in's namespace or class names unless you're the one writing them.

If you publish the interface (from a separate assembly as others have suggested), and I implement your interface, it doesn't matter if my Namespace is Plugin with class MainClass or the namespace is "TheEvilGreebo" and the class name is "IsGod".

As long as my "TheEvilGreebo.IsGod" class IMPLEMENTS your IPlugin properly, that's all that matters from your perspective as the interface designer.

like image 3
The Evil Greebo Avatar answered Oct 16 '22 23:10

The Evil Greebo