I am working on medical software called DTS (Diagnosis and Treatment System) in my free time. This system uses reflection to find ‘functionality extenders’ in a folder. Every time that I develop such plugin-based software, I encounter the same annoying problem and I struggle for a few minutes to resolve it. So I thought I would share my experience and maybe save you some time if you ever experience something similar.
In a seperate assembly called DTSExtendersLib, I define the IDTSExtender so:

I have a seperate project for a test extender, that implements this IDTSExtender interface. So obviously I have added a reference in the test project, to DTSExtendersLib. The test extender is defined like so:

So far so good.
I also have code in DTSExtendersLib that loads extender types and creates instances of those types. That code looks like this:

Good. BUT if you run this, and you attempt to load the test extender, the FindDTSExtenders method will fail on line 38. But why? The IF on line 34 evaluates true, so why does the cast fail?
Now, I was looking through the code again and again, thinking to myself that it MUST work, but still, because it DID NOT work, I was trying to find the bug.
Then something occured to be. .NET can be very finicky when it comes to types. Which is of course part of why .NET is cool. But in this case, what is happening is that my Test Extender references DTSExtendersLib (because it uses the IDTSExtender defined therein), and so it copies (by default setting) the DTSExtendersLib assembly into its output folder. The main application that loads the extenders, obviously also references the DTSExtendersLib assembly, and copies the DTSExtendersLib DLL into its output folder.
And that, my friends, is the problem! Even though exactly the same DTSExtendersLib.dll file existed in the TestExtender folder and the main application folder, the extender loading code failed because the TestExtender was using it’s copy of DTSExtendersLib.dll, and the application was using it’s own local copy of DTSExtendersLib.dll. Same file – but the cast failed.
So I deleted DTSExtendersLib.dll from my TestExtender output folder, and voila! It worked. The cast worked, because there was now only 1 DTSExtendersLib.dll assembly.
Hope this helps someone solve similar problems if doing plug-in type development.