473,725 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.LoadFr om(path + "\\" +
assemblyName);
Object obj = a.CreateInstanc e(className);
PeriodicJob pj = (PeriodicJob)ob j;

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 1620
..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 / DataContractSer ializer / 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
1694
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 info about) the "custom" interfaces InterfaceB
3
1123
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 as plugins for the main app. What I want to do is give some extra functionality to the dlls so that each dll can have some extra functions inside it, that the other dlls are not required to have (so I cannot add more functions to the main...
12
1390
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 register(add) , delete, modify bussiness... I have a class called user, and another called bussiness... Yesterday i was reading about the using of the interfaces and gives to me an
5
3673
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 event logs and other things as I think of them... I've seen two ways of doing this, the abstract class factory, or the interface... the module logic is fairly simple, watch something -> get the result -> notify / fix / and or log it... I've...
9
5199
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
2303
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 able to call methods and pass parameters normally in the dynamically loaded Form. Yesterday Nicholas Paldino's suggested this. Btw, thanks Nick! As hard as I tried without asking further questions, I need a little more help. :)
9
2576
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) example: In Project RoObjDefs: RoPerson.cls file: Public Property Get FirstName() as String Public Property Get LastName() as String <end of file RoPerson.cls> RoPersons.cls file Public Function Count() as Integer
5
1860
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. 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 {}
4
1255
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 the path grabbing the sub-properties down to the "leaf" property at which point the value is returned. The basic concept is this: Function GetValue(ByVal FreightBill As IFreightBill, ByVal ValuePath As String) As Double Dim AssemblyName As...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
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...
0
9111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8096
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
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
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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 we have to send another system
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
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.