PC Review


Reply
Thread Tools Rate Thread

Applying #include semantics in C#

 
 
=?Utf-8?B?U3RldmVfQg==?=
Guest
Posts: n/a
 
      25th Aug 2006
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


 
Reply With Quote
 
 
 
 
Greg Young
Guest
Posts: n/a
 
      25th Aug 2006
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" <(E-Mail Removed)> wrote in message
news:7FC82CA1-C429-420F-B527-(E-Mail Removed)...
>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
>
>



 
Reply With Quote
 
Barry Kelly
Guest
Posts: n/a
 
      25th Aug 2006
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/
 
Reply With Quote
 
Andy
Guest
Posts: n/a
 
      25th Aug 2006
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.

 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      25th Aug 2006

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

 
Reply With Quote
 
=?Utf-8?B?U3RldmVfQg==?=
Guest
Posts: n/a
 
      25th Aug 2006
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.
>
>

 
Reply With Quote
 
Greg Young
Guest
Posts: n/a
 
      25th Aug 2006
Steve there are benefits to keeping it seperate as opposed to the way an
include file works .. namely versioning.

Cheers,

Greg
"Steve_B" <(E-Mail Removed)> wrote in message
news:ED510C72-EB10-4E7A-9124-(E-Mail Removed)...
> 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.
>>
>>



 
Reply With Quote
 
=?Utf-8?B?U3RldmVfQg==?=
Guest
Posts: n/a
 
      25th Aug 2006
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("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
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
array with value semantics =?Utf-8?B?QU1lcmNlcg==?= Microsoft VB .NET 2 4th Nov 2007 08:33 PM
Semantics of BeginXyz()...EndXyz() Markus Ewald Microsoft Dot NET Framework 2 31st May 2006 01:21 PM
ImageLockMode enumeration semantics =?Utf-8?B?TWFyayBSLiBEYXdzb24=?= Microsoft C# .NET 1 3rd Jan 2006 05:19 AM
IEnumerable semantics elvis_the_king@web.de Microsoft C# .NET 3 25th Sep 2005 12:38 PM
it's all about semantics =?Utf-8?B?RGltaXRyaXMgUGFudGF6b3BvdWxvcw==?= Microsoft ASP .NET 2 14th Jul 2004 09:19 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:17 PM.