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 ausing
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:
开发者_如何学Pythonnamespace Plugin
{
public class MainClass : IPlugin //I cannot reference the interface in the main app?
{
public string getName()
{
return "Plugin 1";
}
}
}
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)
You want a class library assembly that contains only the interfaces that both the main application and the plugins are built against.
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.
Just define the IPlugin
interface in a separate assembly.
Also, you should totally be looking at MEF if you're not already: http://mef.codeplex.com/
精彩评论