Applying #include semantics in C#

G

Guest

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();

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.

TIA,
SteveB, MCSD
Welch Allyn
 
B

Barry Kelly

Steve_B said:
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#?

You can put the interface in your .exe file, and have your .dlls
reference the executable.
What constructs in C# provide the same functionality as #include C++?

C#, as you've found, is strongly typed, so there isn't an equivalent
with the same semantics.

-- Barry
 
A

Andy

Barry said:
You can put the interface in your .exe file, and have your .dlls
reference the executable.

Yuck.. that's a horrible idea. A dll shouldn't ever reference an exe..
another dll is the best option.
 
B

Bruce Wood

Steve_B said:
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
 
G

Guest

Yes, I agree that is not a very elegant solution (DLLs referencing the EXE)
but the only one I can think of besides creating a separate (shared) library.
Maybe Anders will pipe in with some insight...
 
G

Greg Young

Steve there are benefits to keeping it seperate as opposed to the way an
include file works .. namely versioning.

Cheers,

Greg
 
G

Guest

Having a background in C++ and COM, reusing an interface is not that
uncommon. But, in the current world of more secure applications I can see
where that's no longer acceptable...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top