473,403 Members | 2,338 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,403 software developers and data experts.

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 1393
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**********@confideltial.comwrote in message
news:e$**************@TK2MSFTNGP04.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(Base o) in Dll2.

The first thing in Dll2 is :
public UseMyDoodah(Base 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.net...
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(Base o) in Dll2.

The first thing in Dll2 is :
public UseMyDoodah(Base 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.net...
>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
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...
8
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...
13
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...
6
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
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...
14
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...
37
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...
5
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...
12
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.