473,657 Members | 2,566 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 MyAbstractInter face

A. Assembly 1
class DerivedClassOne : MyAbstractInter face

B. Assembly 2
class DerivedClassTwo : MyAbstractInter face

C. Application Executable, dynamically loading assemblies from above
MyAbstractInter face someVariable = null;
Assembly assemblyOne = System.AppDomai n.CurrentDomain .Load(...);
MyAbstractInter face classOne =
(MyAbstractInte rface)assembly. CreateInstance( "DerivedClassOn e");
// Use classOne
classOne.SomeMe thod();

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 1268
You would have to put it into a shared library (or have the plugins
reference the original library) ...

LibraryA.ISomet hing != LibraryB.ISomet hing

Cheers,

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

"Steve_B" <St****@discuss ions.microsoft. comwrote in message
news:7F******** *************** ***********@mic rosoft.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 MyAbstractInter face

A. Assembly 1
class DerivedClassOne : MyAbstractInter face

B. Assembly 2
class DerivedClassTwo : MyAbstractInter face

C. Application Executable, dynamically loading assemblies from above
MyAbstractInter face someVariable = null;
Assembly assemblyOne = System.AppDomai n.CurrentDomain .Load(...);
MyAbstractInter face classOne =
(MyAbstractInte rface)assembly. CreateInstance( "DerivedClassOn e");
// Use classOne
classOne.SomeMe thod();

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 ISomethingOrOth er interface that you
wrote with the ISomethingOrOth er interface buried somewhere inside some
Framework DLL. Or, more importantly, the runtime could well confuse the
ISomethingOrOth er 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 MyAbstractInter face

A. Assembly 1
class DerivedClassOne : MyAbstractInter face

B. Assembly 2
class DerivedClassTwo : MyAbstractInter face

C. Application Executable, dynamically loading assemblies from above
MyAbstractInter face someVariable = null;
Assembly assemblyOne = System.AppDomai n.CurrentDomain .Load(...);
MyAbstractInter face classOne =
(MyAbstractInte rface)assembly. CreateInstance( "DerivedClassOn e");
// Use classOne
classOne.SomeMe thod();
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****@discuss ions.microsoft. comwrote in message
news:ED******** *************** ***********@mic rosoft.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 ISomethingOrOth er interface that you
wrote with the ISomethingOrOth er interface buried somewhere inside some
Framework DLL. Or, more importantly, the runtime could well confuse the
ISomethingOrOth er 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 MyAbstractInter face

A. Assembly 1
class DerivedClassOne : MyAbstractInter face

B. Assembly 2
class DerivedClassTwo : MyAbstractInter face

C. Application Executable, dynamically loading assemblies from above
MyAbstractInter face someVariable = null;
Assembly assemblyOne = System.AppDomai n.CurrentDomain .Load(...);
MyAbstractInter face classOne =
(MyAbstractInte rface)assembly. CreateInstance( "DerivedClassOn e");
// Use classOne
classOne.SomeMe thod();

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
13101
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 am curious how compiler find it.
12
1979
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
2422
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 After Applying KB arrticle Still Excel won't quit after subroutine if. oSheet.Copy(oSheet, )
4
1678
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 I want to use the toString() method on them all. Is there a way of defining toString() so that I don't have to loop through all of the particles printing each individually. The code might explain it better than I can so the relevant part of...
13
1492
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 as compared to enclosing each question and answer within divs ? Perhaps , through JavaScript. Thanks in advance ..
6
2074
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 DeviceManager which is only interested in CD/DVD devices. I want to get the list of CD/DVD devices and "be informed when a disc inserted into a device". I am developing this on Linux. So, I used HAL API and read some system (actually /proc) files to gather...
2
1447
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() as double ' the polynomial coefficients ... end structure that will behave like
6
1747
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 wxWidgets - and #include "someheader.h"
6
1994
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 error on them now. The error occurs when I attempt a series of SQL statements wrapped in a TransactionScope(). It's executing against a different server, so this is where it's elevated to a distributed transaction.
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.