473,503 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Applying #include semantics in C#

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("Deri vedClassOne");
// 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
Aug 25 '06 #1
7 1262
You would have to put it into a shared library (or have the plugins
reference the original library) ...

LibraryA.ISomething != LibraryB.ISomething

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

"Steve_B" <St****@discussions.microsoft.comwrote in message
news:7F**********************************@microsof t.com...
>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("Deri vedClassOne");
// 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


Aug 25 '06 #2
Steve_B 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#?
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

--
http://barrkel.blogspot.com/
Aug 25 '06 #3
Barry Kelly wrote:
Steve_B 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#?

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.

Aug 25 '06 #4

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("Deri vedClassOne");
// 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

Aug 25 '06 #5
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...

"Andy" wrote:
Barry Kelly wrote:
Steve_B 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#?
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.

Aug 25 '06 #6
Steve there are benefits to keeping it seperate as opposed to the way an
include file works .. namely versioning.

Cheers,

Greg
"Steve_B" <St****@discussions.microsoft.comwrote in message
news:ED**********************************@microsof t.com...
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...

"Andy" wrote:
>Barry Kelly wrote:
Steve_B 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#?

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.


Aug 25 '06 #7
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...

"Bruce Wood" wrote:
>
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("Deri vedClassOne");
// 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

Aug 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
13086
by: bill | last post by:
Forget the exact definition of difference between, #include <foo.h> and #include "bar.h" Normally foo.h is a standard header file, so it's path is not defined in compiler option, but I...
12
1960
by: Francois Grieu | last post by:
Can #include safely use a preprocessing token, as in #define HEADERFILE "stdio.h" #include HEADERFILE int main(void) {return printf("Hello, world\n")*0;} TIA, François Grieu
7
2413
by: Claudio Benghi | last post by:
Hello World, I've found a problem regarding Interop on Excel 2000. Here's KB article where the problem should be discussed: http://support.microsoft.com/default.aspx?scid=kb;EN-US;317109 ...
4
1669
by: Cleverbum | last post by:
I have created a class Particle which has a method toString() that prints out all the useful information. In my main program I create an array of these objects and once I've fiddled with them a bit...
13
1475
by: vivekian | last post by:
Hi, Have a FAQ page , where have many sets of questions and answers. The questions need to be a different colour / style and the answers different. Was wondering is there a easy way to do this...
6
2058
by: Orgun | last post by:
Hi, I sent this message to the moderated c++ group too but it is waiting for moderator approval and I wanted to send here too. I am new to Design Patterns. I want to write a simple...
2
1437
by: =?Utf-8?B?QU1lcmNlcg==?= | last post by:
I would like to define a structure or a class with an array field that behaves like a simple value-semantics variable. For example, I want something like public structure polynomial public a()...
6
1741
by: Ole Nielsby | last post by:
The standard doesn't define this but what conventions do projects use? As I understand it, #include <somelibrary.h> is used for including system headers and those of frameworks such as...
6
1986
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
Hi - I have 4 webservers in my webfarm. All Win2k3 web edition. Before yesterday, none of them were service packed. I have now applied SP2 to two of them, and I'm getting a very weird MSDTC...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.