Steve_B wrote:
> I've discovered that .NET is so strongly typed that defining the same
> interface in each assembly causes an exception when I cast the object from
> CreateInstance.
Of course it is. How could it be any other way? If it weren't this way,
the runtime could well confuse the ISomethingOrOther interface that you
wrote with the ISomethingOrOther interface buried somewhere inside some
Framework DLL. Or, more importantly, the runtime could well confuse the
ISomethingOrOther interface written by some hacker with the one that
you wrote.
> I would like to dynamically load assemblies that implement a single
> interface. I would like to reference a single definition of an interface
> without creating and deploying a seperate assembly. How is this done in C#?
> What constructs in C# provide the same functionality as #include C++?
>
> For example:
> Declared interface
> interface MyAbstractInterface
>
> A. Assembly 1
> class DerivedClassOne : MyAbstractInterface
>
> B. Assembly 2
> class DerivedClassTwo : MyAbstractInterface
>
> C. Application Executable, dynamically loading assemblies from above
> MyAbstractInterface someVariable = null;
> Assembly assemblyOne = System.AppDomain.CurrentDomain.Load(...);
> MyAbstractInterface classOne =
> (MyAbstractInterface)assembly.CreateInstance("DerivedClassOne");
> // Use classOne
> classOne.SomeMethod();
The solution is to put the interface definition itself into a separate
DLL. See Jon Skeet's article here:
http://www.yoda.arachsys.com/csharp/plugin.html