I'm writing a plugin architecture for testing purposes (I know there are frameworks to do that), this is how I've done it right now:
I have a common library shared by the main program and the plugins dll, I'll call it PluginBase assembly.
That assembly is referenced in a plugin MyPlugin which is in the folder Debug\Plugins.
It is also referenced in the main program, which is in the folder Debug.
The problem is, when I dynamically load the interfaces contained in the PluginBase, it gets the interface's Type object from the assembly located in Debug\Plugins.
But when I try to use the same interface (IMyPlugin) in the main program, it loads it from the PluginBase assembly located in the Debug folder.
And that causes invalid cast exceptions when I try to use the interface in the main Program.
The folder structure:
\Debug\MainProgram.exe
\Debug\PluginBase.dll
\Debug\Plugins\MyPlugin.dll
\Debug\Plugins\PluginBase.dll
The scenario of the loading is:
- The
ServiceProviderloads the plugins definition, ans also dynamically loads the interfaces contained in thePluginBaseassembly (located inDebug\Plugins) - The main program calls the
ServiceProviderto get a specific interface (but this time loaded from theDebugfolder) - The types of the interfaces don't match (even if they are the same)....
I tried to locate the PluginBase dll in the Debug folder, but then I get compilation issues since the PluginBase dll gets copied to the Debug folder after the compilation of my plugin project.
Also I don't think this is a clean way to do this.
Is there a way to overcome this problem ? (like perhaps setting a build order in a way?)