473,387 Members | 3,801 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,387 software developers and data experts.

Recognizing interfaces while using reflection

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 load the
assembly with late binding and create an instance of the class with
reflection. I try to cast the object to the class type of the
interfact, but I get an invalid cast exception. It says that the
object I am creating isn't a PeriodicJob. But the class does
implement the PeriodicJob interface. I am guessing the reason it
doesn't work is because when I declare the interface in the DLL and
then declare it again in the EXE, even though they have the same name
and structure, they are considered to be two different interfaces.
Here's the code where I use reflection:

Assembly a = Assembly.LoadFrom(path + "\\" +
assemblyName);
Object obj = a.CreateInstance(className);
PeriodicJob pj = (PeriodicJob)obj;

How do I make it such that both the DLL and the EXE use the same
actual interface? Do I have to declare the interface in a totally
different DLL and then reference that DLL from both my EXE and DLL
that has the implementation?

thanks in advance,
John
Nov 4 '08 #1
1 1580
..NET types are always scoped by their assembly. So yes; even if two
projects use the same .cs file, they are two different .NET types, and
cannot be cast to eachother. For runtime purposes, the trick is to
place the code into an assembly that both of your *actual* assemblies
can see. This might be a new dll, or it could just be that the exe
references the dll (and place the interface in the dll).

For serialization purposes, the best approach (for scenarios where it
must be interoperable with different clients) is to use something that
doesn't depend on .NET types - so BinaryFormatter might be
inappropriate, but XmlSerializer / DataContractSerializer / bespoke
might be viable options.

Marc
Nov 4 '08 #2

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

Similar topics

3
by: Cezar | last post by:
Hello, Can anyone please help me with this matter. I try to find the interfaces implemented by a class using Reflection. e.g: public class A : InterfaceB, InterfaceC how can I get (the...
3
by: pnp | last post by:
Hi all, I've created an app that loads some dlls dynamically from the filesystem. All the dlls implement a certain interface so that they can be easily manipulated from the main app. The dlls work...
12
by: Josema | last post by:
Hi, Im starting develop a portal, and i never used interfaces in the past. As any portal will be possible register (add), delete, modify a user. In this portal also will be possible...
5
by: Michael McCarthy | last post by:
I want to develop plugin support for a system.montitor module I am working on. A lot of the modules will do mostly interop stuff for an older system, but I want to use it myself as well to monitor...
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
1
by: John F | last post by:
Hello All, I'm dynamically loading a form through reflection. I have this working. What I'd like to do is implement an interface so that I can cast my instance to that interface. I want to be...
9
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
5
by: | last post by:
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....
4
balabaster
by: balabaster | last post by:
I've got a hierarchy of interfaces that I need passed into a method to return a value given a value path. For instance: Class.Property.SubProperty.Value I'm using reflection to iterate through...
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: 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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.