HighTechTalks DotNet Forums  

InvalidCastException

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss InvalidCastException in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Lars
 
Posts: n/a

Default InvalidCastException - 05-29-2006 , 08:39 AM






Hi,

I get a InvalidCastException:
- ex {"Unable to cast object of type 'SecSoaService.NFFIServiceParameters'
to type 'SecSoaService.NFFIServiceParameters'."} System.Exception
{System.InvalidCastException}

The Object beeing casted originates from a Serialize / Deserialize
roundtrip. I have tried with both binary and soap serializer with no luck.

This is NOT ASP 2.0, but pure c#. I have seen similiar problem in blogs, but
all of these relates to ASP 2.0.

To avoid version mismatch I have located all copies of the dll on all local
hard drives and deleted them.

Lars.



Reply With Quote
  #2  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: InvalidCastException - 05-29-2006 , 02:02 PM






Lars <lars (AT) nospam (DOT) com> wrote:
Quote:
I get a InvalidCastException:
- ex {"Unable to cast object of type 'SecSoaService.NFFIServiceParameters'
to type 'SecSoaService.NFFIServiceParameters'."} System.Exception
{System.InvalidCastException}
Does that type exist in multiple assemblies? If so, that's probably the
issue.

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Reply With Quote
  #3  
Old   
Lars
 
Posts: n/a

Default Re: InvalidCastException - 05-29-2006 , 03:38 PM



Hi,

No, the type is not defined in another assembly, nor is there any other cached assemblies lying around.

This is the code that fails:

The type SecSoaService.NFFIServiceParameters inherits from the abstract class ServiceParameters.
public ServiceParameters EditWebServiceParameters(ServiceParameters parameters)
{
SecSoaService.NFFIServiceParameters param = null;
try
{
param = (SecSoaService.NFFIServiceParameters)parameters; //exception is thrown.
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message);
}

// test
try
{
param = (SecSoaService.NFFIServiceParameters)(new SecSoaService.NFFIServiceParameters()); //no exception.
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message);
}
...
}

From the watch window i extract the following:
parameters.GetType().Module.FullyQualifiedName = "c:\\temp\\IFM\\Tool\\modules\\services\\secsoa\\S ecSoaService.dll" string
and the
parameters.GetType().AssemblyQualifiedName = "SecSoaService.NFFIServiceParameters, SecSoaService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e9c77c3cc6a7a0ac"

And the param variable gets the following values after beeing assigned:
param.GetType().Module.FullyQualifiedName = "C:/temp/IFM/Tool/modules/services/secsoa\\SecSoaService.dll" string

param.GetType().AssemblyQualifiedName = "SecSoaService.NFFIServiceParameters, SecSoaService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e9c77c3cc6a7a0ac"

As you can see both types have the same Assembly Qualified name and the same Module FullyQualifiedName.

So as far as i can see these two types are identical!
The only thing that is different is the format of the FullyQualifiedName path.

regards,
Lars





"Jon Skeet [C# MVP]" <skeet (AT) pobox (DOT) com> wrote

Quote:
Lars <lars (AT) nospam (DOT) com> wrote:
I get a InvalidCastException:
- ex {"Unable to cast object of type 'SecSoaService.NFFIServiceParameters'
to type 'SecSoaService.NFFIServiceParameters'."} System.Exception
{System.InvalidCastException}

Does that type exist in multiple assemblies? If so, that's probably the
issue.

--
Jon Skeet - <skeet (AT) pobox (DOT) com
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Reply With Quote
  #4  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: InvalidCastException - 05-31-2006 , 02:55 AM



Lars <lars (AT) nospam (DOT) com> wrote:
Quote:
No, the type is not defined in another assembly, nor is there any other cached assemblies lying around.
It looks like they're loaded into different Assembly instances though,
given the fully qualified name differences. (Even if everything was the
same, being in different Assembly instances would be a problem.)

Are either of these assemblies loaded explicitly using Assembly.Load?

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Reply With Quote
  #5  
Old   
Lars
 
Posts: n/a

Default Re: InvalidCastException - 05-31-2006 , 06:14 AM



You nailed the problem

I changed from LoadFile to LoadFrom to enforce same context.

Thanks alot,
Lars

"Jon Skeet [C# MVP]" <skeet (AT) pobox (DOT) com> wrote

Quote:
Lars <lars (AT) nospam (DOT) com> wrote:
No, the type is not defined in another assembly, nor is there any other
cached assemblies lying around.

It looks like they're loaded into different Assembly instances though,
given the fully qualified name differences. (Even if everything was the
same, being in different Assembly instances would be a problem.)

Are either of these assemblies loaded explicitly using Assembly.Load?

--
Jon Skeet - <skeet (AT) pobox (DOT) com
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.