473,657 Members | 2,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can't cast ABC to ABC (same class???)

Hi,

Under what circumstances can I ever get an exception about not being able to
cast type "ABC" to type "ABC" if there's only one ABC class in the system
and it's loaded from a dynamic assembly?

Thanks
Ron
Nov 21 '06 #1
6 1403
I have seen this type of behaviour.

For us it occurred under the following circumstances :
When deployed some dll's are not stored in the same directory as the exe.
(We used the config options to set the exe to look in subdirectories for
dll's)
The same dll exists in 2 different folders.

Check to see if your app can find more than one copy of the same dll. Of
course, if all your dlls are in the same folder as your dll what I've said
doesn't apply.

HTH,

Adam.
========
"Ron M. Newman" <co**********@c onfideltial.com wrote in message
news:e$******** ******@TK2MSFTN GP04.phx.gbl...
Hi,

Under what circumstances can I ever get an exception about not being able
to cast type "ABC" to type "ABC" if there's only one ABC class in the
system and it's loaded from a dynamic assembly?

Thanks
Ron


Nov 22 '06 #2
Ron M. Newman wrote:
Under what circumstances can I ever get an exception about not being able to
cast type "ABC" to type "ABC" if there's only one ABC class in the system
and it's loaded from a dynamic assembly?
You don't have the ABC class code duplicated anywhere do you? That
would cause it.

Nov 22 '06 #3
>if all your dlls are in the same folder as your dll
I meant "if all your dlls are in the same folder as your app exe"
Nov 22 '06 #4
On Tue, 21 Nov 2006 11:49:27 -0500, Ron M. Newman wrote:
Hi,

Under what circumstances can I ever get an exception about not being able to
cast type "ABC" to type "ABC" if there's only one ABC class in the system
and it's loaded from a dynamic assembly?

Thanks
Ron
If ABC is only in a dynamically loaded class then how are you casting to
it? There can't be a cast unless there is a reference to it, as casting is
a compile time concept, not runtime.
The only cast you can do is to have a third shared DLL with a common
interface 'X', and implement it in your external class, then you can cast
to type 'X' in your main application.
Now assuming you have something like this, which as your code is compiling
I assume you have, then you must make sure the signature used by both main
and external DLLs is the same, and the best way to do this is to put your
interfaces in a separate project and reference it from both other DLLs.

Cheers,
Gadget
Nov 22 '06 #5
If ABC is only in a dynamically loaded class then how are you casting to
it?
The main exe uses a base class, say Base.
Dll1 (no references for this in the main exe build) extends Base to Derived.
Dll2 and Dll3 create \ use Derived and both support an interface known by
the main exe.

The main exe loads up the dlls at run time and, through the interface, calls
Base GiveMeADoodah() in Dll1.
It then calls UseMyDoodah(Bas e o) in Dll2.

The first thing in Dll2 is :
public UseMyDoodah(Bas e o)
{
Derived d = (Derived) o;
...
}

And I would guess that line is where his problem his. The solution for us
was to ensure all dlls existed only in one place. When we had this issue
Dll2 and Dll3 were in 2 different sub-dirs (which was fine), but Dll1 had
somehow been deployed into both sub-directories (not fine.) It seemed as
though creation of the object involved using the dll from one sub-directory,
usage of the object involved the dll in a different sub-directory. Even
though it was the same dll you couldn't use an object created by one in the
other. (It helps if you cross your eyes and stand on your head when reading
this.)

Adam.

"Gadget" <ga****@sobell. netwrote in message
news:92******** *************** ******@40tude.n et...
On Tue, 21 Nov 2006 11:49:27 -0500, Ron M. Newman wrote:
>Hi,

Under what circumstances can I ever get an exception about not being able
to
cast type "ABC" to type "ABC" if there's only one ABC class in the system
and it's loaded from a dynamic assembly?

Thanks
Ron

If ABC is only in a dynamically loaded class then how are you casting to
it? There can't be a cast unless there is a reference to it, as casting is
a compile time concept, not runtime.
The only cast you can do is to have a third shared DLL with a common
interface 'X', and implement it in your external class, then you can cast
to type 'X' in your main application.
Now assuming you have something like this, which as your code is compiling
I assume you have, then you must make sure the signature used by both main
and external DLLs is the same, and the best way to do this is to put your
interfaces in a separate project and reference it from both other DLLs.

Cheers,
Gadget

Nov 23 '06 #6
I think I get what you mean :)
I suspect .NET's 'official' solution to this is to ensure your DLL defining
'Base' is registered in the GAC.

Cheers,
Gadget

On Thu, 23 Nov 2006 10:10:58 -0000, Adam Benson wrote:
>If ABC is only in a dynamically loaded class then how are you casting to
it?

The main exe uses a base class, say Base.
Dll1 (no references for this in the main exe build) extends Base to Derived.
Dll2 and Dll3 create \ use Derived and both support an interface known by
the main exe.

The main exe loads up the dlls at run time and, through the interface, calls
Base GiveMeADoodah() in Dll1.
It then calls UseMyDoodah(Bas e o) in Dll2.

The first thing in Dll2 is :
public UseMyDoodah(Bas e o)
{
Derived d = (Derived) o;
...
}

And I would guess that line is where his problem his. The solution for us
was to ensure all dlls existed only in one place. When we had this issue
Dll2 and Dll3 were in 2 different sub-dirs (which was fine), but Dll1 had
somehow been deployed into both sub-directories (not fine.) It seemed as
though creation of the object involved using the dll from one sub-directory,
usage of the object involved the dll in a different sub-directory. Even
though it was the same dll you couldn't use an object created by one in the
other. (It helps if you cross your eyes and stand on your head when reading
this.)

Adam.

"Gadget" <ga****@sobell. netwrote in message
news:92******** *************** ******@40tude.n et...
>On Tue, 21 Nov 2006 11:49:27 -0500, Ron M. Newman wrote:
>>Hi,

Under what circumstances can I ever get an exception about not being able
to
cast type "ABC" to type "ABC" if there's only one ABC class in the system
and it's loaded from a dynamic assembly?

Thanks
Ron

If ABC is only in a dynamically loaded class then how are you casting to
it? There can't be a cast unless there is a reference to it, as casting is
a compile time concept, not runtime.
The only cast you can do is to have a third shared DLL with a common
interface 'X', and implement it in your external class, then you can cast
to type 'X' in your main application.
Now assuming you have something like this, which as your code is compiling
I assume you have, then you must make sure the signature used by both main
and external DLLs is the same, and the best way to do this is to put your
interfaces in a separate project and reference it from both other DLLs.

Cheers,
Gadget
Nov 23 '06 #7

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

Similar topics

4
3734
by: Clint Hill | last post by:
I am working on a project that I would like to have some extensibility for later. So with this I am using interfaces to pass objects to derived classes. However I am running into a situation where two seperate classes won't cast each other even though they implement the same interface. The classes are identical yet won't convert. Here is (briefly) what I am talking about: public interface IReturnObject { Guid ID { get; set; } string...
8
2501
by: Zheng Da | last post by:
I don't know where should I ask the question, so send the email to this group. I choose this group, because I want to write the program with c++ :) I want to write a program which support multiprotocol, but do not want to write code for all protocols which I want to support. I plan I give a interface and others give a module which implements a protocol, and then the module can be inserted into my program without recompiling my program....
13
5040
by: bonk | last post by:
Hello, I am trying to create a dll that internally uses managed types but exposes a plain unmanaged interface. All the managed stuff shall be "wrapped out of sight". So that I would be able to use that dll from pure unmanaged code (for example inherit from classes in that dll). Is something like that possible. I heared something called ManWarp tried that approach. If it is possible, how can I do that. Maybe there is a small litttle
6
6721
by: Lore Leunoeg | last post by:
Hello I derived a class MyControl from the Control class. Public Class MyControl Inherits Control Sub New() MyBase.New() End Sub End Class
6
3543
by: Bill foust | last post by:
I'm running into a situation there I think an operator overload would solve the issue, but I'm unable to make it work for some reason. If anyone can help here I would appreciate it. I have a base class that is common to many other classes. public class Base .... end class I have 2 seperate classes that inherit from base
14
18739
by: budy_ludy | last post by:
Hi All, I am new to vb .net, I have an ArrayList and i store class objects in it, and later i want to retrieve each ArrayList items and type cast to the class, How can it be done ? I used CType for casting but it is throwing exception.
37
3925
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as a function
5
2381
by: taumuon | last post by:
I've got an object, Person, that supports IEquatable<Person>. It implements bool Equals(Person obj) as well as overriding bool Equals(object obj) I've got a container type that holds a member object of generic type T, that supports IEquatable<T>, and a method, DoComparisons(T obj) to compare the member object to the object passed in.
12
2162
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have a class which "BiologySequence" which looks about like this. public class BiologySequence { private string _Sequence; public string Sequence {
0
8397
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
8310
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
8827
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
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...
0
4158
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
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2731
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
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1620
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.