1

I've done this before in C# for a plugin system which works fine, which is why I'm puzzled as to why this new, separate plugin system is not working the way I would expect.

I have my plugin assembly - let's call it "plugin.dll" and I have my main assembly - let's call it App.exe.

I have an interface called IMyObject and its implementation, MyObject and both are defined in plugin.dll. I've copied the exact code file for IMyObject (which the plugin developer has provided to me) into my main App.exe assembly.

I get an InvalidCastException when I try to cast the object I load using reflection to the interface that the object implements. I know the object implements it because

t.GetInterface(typeof(IMyObject).FullName) != null

is true. I can also browse MyObject in the object explorer in Visual Studio and I can see that it implements IMyObject.

Here's where it goes wrong:

 if (ifaceType != null)
 {
    ConstructorInfo constructor = ifaceType.GetConstructor(new Type[] { });

    if (constructor != null)
    {

       object obj = constructor.Invoke(null); // this works - obj is assigned an 
                                              // instance of MyObject

       IMyObject myObj = (IMyObject)obj;      // triggers InvalidCastException
    }               
 }

The only difference that I can see between what I'm doing now and the way I implemented this before is that the interface is defined in two separate assemblies (even though the code files are identical and they belong to the same namespace).

Is this the reason for my trouble? If so, how can I connect to my plugin without linking at compile time, and use an interface that is defined in the plugin itself?

I should add that aside from the interface, I do not have access to the source code for the plugin.

8
  • 1
    Have you considered a third assembly that you can both reference? I'm not 100% sure what you're attempting to do will "just work". Nov 21, 2012 at 5:47
  • That's the way I did it once before and that works, but it this time, circumstances are such that I do not have access to the plugin code other than the interface itself. Nov 21, 2012 at 5:50
  • What you're saying doesn't make any sense. You don't NEED access to the plugin code. You expose an interface and load types that implement the interface from an assembly. Then you call functions against the interface.. how does that translate to "You need to have access to the plugin code".. ? Nov 21, 2012 at 5:56
  • I don't mean to be difficult. If I had access to the code, I could indeed add a common, third assembly and both my main assembly and the plugin could reference that common assembly. As I said, I don't have access to the plugin code and as such, cannot compile the plugin myself. Nov 21, 2012 at 6:02
  • So, you're attempting to load a piece of code from another assembly by copying an interface in that assembly and dynamically loading it? How do you know that the interface exists in the other assembly? Nov 21, 2012 at 6:05

4 Answers 4

3

Even though those interfaces have the same code, they are two separate interfaces in two different assemblies (you could print their AssemblyQualifiedNames and see the difference).

3
  • If I used it, it would not be a late-bound plugin, would it? Nov 21, 2012 at 5:47
  • sorry, missed that piece in the title. There's really no way to load a type (i.e an interface) at run-time and use it in a strongly (statically) typed manner. You can use a common interfaces library or just use dynamic instead of an interface. Nov 21, 2012 at 6:09
  • dynamic isn't an option since we're both using .Net 2.0. It's looking like I'm going to have to supply a DLL to the plugin developer with an interface that I define and that we both reference. Nov 21, 2012 at 6:13
2

Yes, the interfaces are considered different types by the runtime. They have the same name that is all.

There is no way to use the interface in the host program without reflection because you can't statically link against it. Typically, one puts interfaces in the plugin host assembly and plugins implement them, not the other way around.

3
  • The plugin developer does not have access to my code, though. Otherwise, yes I could define the interface. Nov 21, 2012 at 5:52
  • @CharlieSalts How does the plugin developer write a plugin then? Surely they must implement an IPlugIn or something. Or is it all completely dynamic? Nov 21, 2012 at 6:05
  • I assumed that if we used the same interface it would work (the way it would with C header files). All the method names are the same and the class name is the same and so on. As someone else pointed out, this does not make them equal. Nov 21, 2012 at 6:08
1

Typical skeleton of extensible application in .NET consists of these assemblies:

  • contracts assembly. This one contains contracts (just interfaces and, optionally, some data contracts, used in interfaces) for plugins, and contracts for host;
  • host assembly. This one contains host contracts implementations and depends from the first one;
  • several plugin assemblies. These assemblies contain plugin contracts implementations and depend from the first one too.

Thereby, using contracts assembly, you're achieving, that host and plugins are independent from each other (so, plugins are late-bound), but they have common contracts, known at compile-time for both sides (plugin and host).

The way you're going (define plugin contact multiple times) is a wrong way.

1

As others have mentioned these are separate interfaces and the correct way to handle this is to put the definition into a third Assembly that both reference.

BUT ... there is a way to get this to work by using a library called Impromptu Interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.