all groups > dotnet clr > may 2006 >
You're in the

dotnet clr

group:

InvalidCastException


InvalidCastException Lars
5/29/2006 2:39:20 PM
dotnet clr:
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.

Re: InvalidCastException Jon Skeet [C# MVP]
5/29/2006 7:02:08 PM
[quoted text, click to view]

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: InvalidCastException Lars
5/29/2006 9:38:10 PM
Hi,

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

This is the code that fails:

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

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

From the watch window i extract the following:
parameters.GetType().Module.FullyQualifiedName =3D =
"c:\\temp\\IFM\\Tool\\modules\\services\\secsoa\\SecSoaService.dll" =
string
and the=20
parameters.GetType().AssemblyQualifiedName =3D =
"SecSoaService.NFFIServiceParameters, SecSoaService, Version=3D1.0.0.0, =
Culture=3Dneutral, PublicKeyToken=3De9c77c3cc6a7a0ac"

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

param.GetType().AssemblyQualifiedName =3D =
"SecSoaService.NFFIServiceParameters, SecSoaService, Version=3D1.0.0.0, =
Culture=3Dneutral, PublicKeyToken=3De9c77c3cc6a7a0ac"=20

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. =20

regards,
Lars

=20



[quoted text, click to view]
Re: InvalidCastException Jon Skeet [C# MVP]
5/31/2006 12:00:00 AM
[quoted text, click to view]

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@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: InvalidCastException Lars
5/31/2006 12:14:58 PM
You nailed the problem :)

I changed from LoadFile to LoadFrom to enforce same context.

Thanks alot,
Lars

[quoted text, click to view]

AddThis Social Bookmark Button