473,385 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using interfaces for reflection-loaded classes

I am having problems with casting or converting a class to to an interface
from which it derives. I'm certain that it's due to how it's being loaded,
but I'm not sure how to get past the problem.

Here's a general outline of the architecture. It's oversimplified, but I
think it's enough info to help:

Assembly A.dll
{
interface IMyBase {}
}

Assembly B.dll (references A.dll)
{
class PlugIn : IMyBase {}
}

Assembly C.exe (references A.dll)
{
class InternalClass : IMyBase {}

class MyUI
{
// use IMyBase stuff here
}
}
Now if C.exe loads up an instance of InternalClass, it can cast it to
IMyBase and use it no problem.

If it loads B.dll and creates an instance of PlugIn through reflection I
*cannot* then cast it to an IMyBase.

How can I get access to the dynamically loaded plug in instance's members as
the base interface type? Til now I've been hacking it and just doing things
like getting functions by name, not by using the interface contract, but now
I need to create a handler for an event in the plug in and my hacks are
getting really ugly.

-Chris
Sep 25 '07 #1
5 1840

I'm getting ready to leave for the day, but

http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!126.entry
I have an example there.

But just to clarify, you saying that in B.dll
the following code fails
PlugIn pi = new PlugIn ();

IMyBase ipi = pi as IMyBase;

if(null!=ipi)
{
ipi.SomeMethod();
}
?


"<ctacke/>" <ctacke[at]opennetcf[dot]comwrote in message
news:Ou**************@TK2MSFTNGP02.phx.gbl...
>I am having problems with casting or converting a class to to an interface
from which it derives. I'm certain that it's due to how it's being loaded,
but I'm not sure how to get past the problem.

Here's a general outline of the architecture. It's oversimplified, but I
think it's enough info to help:

Assembly A.dll
{
interface IMyBase {}
}

Assembly B.dll (references A.dll)
{
class PlugIn : IMyBase {}
}

Assembly C.exe (references A.dll)
{
class InternalClass : IMyBase {}

class MyUI
{
// use IMyBase stuff here
}
}
Now if C.exe loads up an instance of InternalClass, it can cast it to
IMyBase and use it no problem.

If it loads B.dll and creates an instance of PlugIn through reflection I
*cannot* then cast it to an IMyBase.

How can I get access to the dynamically loaded plug in instance's members
as the base interface type? Til now I've been hacking it and just doing
things like getting functions by name, not by using the interface
contract, but now I need to create a handler for an event in the plug in
and my hacks are getting really ugly.

-Chris


Sep 25 '07 #2

The oversimplified code works fine, assembly B can be loaded through
reflection (Assembly.LoadFile for example) and then class PlugIn can
be cast directly to IMyBase.

So there has to be something different between the oversimplified case
and your actual code. I would suggest either building upon the
oversimplified case so it approaches your real code until it breaks
and seeing what caused the break, or working the other way and try to
comment out the real code until it works. Either way, we can't help
without knowing your code 'cause the sample you provided works fine.

Besides this oversimplified example, we use this type of reflection
loaded plugin system extensively in our applications and have not had
a problem casting instances to their interfaces.

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Tue, 25 Sep 2007 16:17:08 -0500, "<ctacke/>"
<ctacke[at]opennetcf[dot]comwrote:
>I am having problems with casting or converting a class to to an interface
from which it derives. I'm certain that it's due to how it's being loaded,
but I'm not sure how to get past the problem.

Here's a general outline of the architecture. It's oversimplified, but I
think it's enough info to help:

Assembly A.dll
{
interface IMyBase {}
}

Assembly B.dll (references A.dll)
{
class PlugIn : IMyBase {}
}

Assembly C.exe (references A.dll)
{
class InternalClass : IMyBase {}

class MyUI
{
// use IMyBase stuff here
}
}
Now if C.exe loads up an instance of InternalClass, it can cast it to
IMyBase and use it no problem.

If it loads B.dll and creates an instance of PlugIn through reflection I
*cannot* then cast it to an IMyBase.

How can I get access to the dynamically loaded plug in instance's members as
the base interface type? Til now I've been hacking it and just doing things
like getting functions by name, not by using the interface contract, but now
I need to create a handler for an event in the plug in and my hacks are
getting really ugly.

-Chris
Sep 26 '07 #3
I'll look at it further tomorrow, but the "more true" scenario is this:

We have an app (C) that gets the interface from a separate assembly (A).
We have a plug in (B) that also gets the interface from A.

The app loads a class from the plug in. This class exposes a "GetForm"
method. The Form returned from this inherits Form plus implements IPlugIn.
It can be used as a Form, as we're able to display it just fine, however it
just doesn't like that interface. I can get the interface by name, so the
class definitely implements it, but it won't cast. It's like it sees it as
a separate interface, just with coincidentally the same interface name.

-Chris

"Samuel R. Neff" <sa********@nomail.comwrote in message
news:09********************************@4ax.com...
>
The oversimplified code works fine, assembly B can be loaded through
reflection (Assembly.LoadFile for example) and then class PlugIn can
be cast directly to IMyBase.

So there has to be something different between the oversimplified case
and your actual code. I would suggest either building upon the
oversimplified case so it approaches your real code until it breaks
and seeing what caused the break, or working the other way and try to
comment out the real code until it works. Either way, we can't help
without knowing your code 'cause the sample you provided works fine.

Besides this oversimplified example, we use this type of reflection
loaded plugin system extensively in our applications and have not had
a problem casting instances to their interfaces.

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Tue, 25 Sep 2007 16:17:08 -0500, "<ctacke/>"
<ctacke[at]opennetcf[dot]comwrote:
>>I am having problems with casting or converting a class to to an interface
from which it derives. I'm certain that it's due to how it's being
loaded,
but I'm not sure how to get past the problem.

Here's a general outline of the architecture. It's oversimplified, but I
think it's enough info to help:

Assembly A.dll
{
interface IMyBase {}
}

Assembly B.dll (references A.dll)
{
class PlugIn : IMyBase {}
}

Assembly C.exe (references A.dll)
{
class InternalClass : IMyBase {}

class MyUI
{
// use IMyBase stuff here
}
}
Now if C.exe loads up an instance of InternalClass, it can cast it to
IMyBase and use it no problem.

If it loads B.dll and creates an instance of PlugIn through reflection I
*cannot* then cast it to an IMyBase.

How can I get access to the dynamically loaded plug in instance's members
as
the base interface type? Til now I've been hacking it and just doing
things
like getting functions by name, not by using the interface contract, but
now
I need to create a handler for an event in the plug in and my hacks are
getting really ugly.

-Chris


Sep 26 '07 #4
<ctacke/wrote:
I'll look at it further tomorrow, but the "more true" scenario is this:

We have an app (C) that gets the interface from a separate assembly (A).
We have a plug in (B) that also gets the interface from A.

The app loads a class from the plug in. This class exposes a "GetForm"
method. The Form returned from this inherits Form plus implements IPlugIn.
It can be used as a Form, as we're able to display it just fine, however it
just doesn't like that interface. I can get the interface by name, so the
class definitely implements it, but it won't cast. It's like it sees it as
a separate interface, just with coincidentally the same interface name.
I really have no practical experience with this sort of thing. However,
the last time something like this came up in this newsgroup, it turned
out to be an issue where the class being used for the cast was just as
you describe: it had the same name, but was from a completely different
assembly.

You should double-check to make sure that the interface that all code is
using is the same interface. That is, all of the code referencing the
interface uses the same assembly, compiled from a single source. The
problem _sounds_ like you may have an issue where you're getting the
interface from multiple places.

Pete
Sep 26 '07 #5
I concur.

I've seen that issue as well, where it was the same name, but actually
different classes.

Can you get some extra info about it , using something like ... GetType()
and the full AssemblyName or something like that
(its late and I'm going from memory)


"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
<ctacke/wrote:
>I'll look at it further tomorrow, but the "more true" scenario is this:

We have an app (C) that gets the interface from a separate assembly (A).
We have a plug in (B) that also gets the interface from A.

The app loads a class from the plug in. This class exposes a "GetForm"
method. The Form returned from this inherits Form plus implements
IPlugIn. It can be used as a Form, as we're able to display it just fine,
however it just doesn't like that interface. I can get the interface by
name, so the class definitely implements it, but it won't cast. It's
like it sees it as a separate interface, just with coincidentally the
same interface name.

I really have no practical experience with this sort of thing. However,
the last time something like this came up in this newsgroup, it turned out
to be an issue where the class being used for the cast was just as you
describe: it had the same name, but was from a completely different
assembly.

You should double-check to make sure that the interface that all code is
using is the same interface. That is, all of the code referencing the
interface uses the same assembly, compiled from a single source. The
problem _sounds_ like you may have an issue where you're getting the
interface from multiple places.

Pete

Sep 26 '07 #6

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

Similar topics

4
by: BrianProgrammer | last post by:
I have this code below, that works like a champ, but two lines are continually marked as obsolete. See embeded notes. Private Shared Function TransformHTMLString(ByVal XSLT As String, _ ByVal...
3
by: J E E | last post by:
Hi! Is it possible to access fields in a derived class using reflection? Code below works fine when I access it as a private member in the Page class, but not when accessing base class member...
11
by: Ian | last post by:
Hi, I am writing an application and am creating a plug-in interface for it. The application will need to 'find' the plug-in's and will then need to find all accessible methods. The...
5
by: Eran Kampf | last post by:
I was wondering why MS defined "marker" interfaces (empty interfaces) in the ..NET framework(interfaces such as such as INamingContainer, IRequiresSessionState etc...). Why not use attributes? ...
5
by: Eric Goforth | last post by:
Hello, I have a generic subroutine that I pass an object and fieldname as arguments. The subroutine then uses reflection to search for the value of the fieldname. For example: 'Calling...
2
by: Trapulo | last post by:
I have a reference to an object via a specific interface. I need to read a propery of this object via reflection, and I have only the property name. My problem is that I cannot retrieve a type...
2
by: Rob | last post by:
I'm using reflection to inspect a class and its methods. Is is possible to determine if the class is a derived class or an interface implementation? I'm using the MethodInfo object for relfection...
2
by: Stefan | last post by:
Hello, I was hoping my fellow coders would give me some feedback on the article I wrote which makes use of the System.Reflection namespace and Polymorphism to demonstrate how you can create...
0
by: Rajesh | last post by:
Hi, I have a vc++ com component all the business validations are done through the component.Im using interop to communicate with the com component through ..Net(C#) I want to design a class...
1
by: Big Daddy | last post by:
I have an interface called PeriodicJob. In a DLL, I declare the interface and then create a class that implements the interface. In a different executable, I also declare the interface. I then...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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...
0
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...

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.