Strange Deserialization problem.

T

TheSteph

Hi,

I have a small serializable struct :

[Serializable]
public struct TestStruct
{
public string Title;
public int Age;
public string Name;
public string Surname;
}

I "binary" Serialize it in a file without any problem.
I can desreialize it in the SAME application with NO problems.
But when I want to Deserialize it in an OTHER application, I get an Invalide
cast error :

TestStruct TmpTestStruct = (TestStruct)formatter.Deserialize(fs);

=> Error message "Specified cast is not valid" !

So, I tested to deserialize it in an object, and check the Type :

Object TmpObj = formatter.Deserialize(fs);
MessageBox.Show(TmpObj.GetType().ToString());

It works fine, and the MessageBox show "TestStruct". but impossible to cast
it as TestStruct !! ("Specified cast is not valid")

So why is it deserializable in the same Application and not in an other,
even if both use the same source ??

Can anybody help me ? Any idea of what I am doing wrong ? a MS Bug ? Is it
a problem because of different namesapce in both applications ?

Any help appreciated.

Steph.
 
N

Nicholas Paldino [.NET/C# MVP]

Steph,

Do you have a reference in the other application to the TestStruct
structure, or did you copy the code? If you copied the code, then in
essence, you have created a NEW type. It has the same logical structure but
it is a different type to the CLR.

If you are using .NET 3.0, then you can use the DataContract attribute
along with the DataContractSerializer class to write your files and read
them. This will allow you to have separate CLR types which have the same
logical structure and read them from the file.

You can also achieve the same thing with serialization in .NET 2.0 using
serialization surrogates, but it's pretty messy.

Also, for this kind of structure, you could get away with Xml
Serialization as well, which works in .NET 2.0 and earlier.

Of course, if you take the type and store it in an assembly that is
referenced by both executables, then all of this is moot.

Hope this helps.
 
T

TheSteph

Many thanks for your fast, complete and instructive reply !



I actually copied the code that define my TestStruct. So the problem must
be there. I didn't thought a second that the CLR will refuse to deserialize
from one Type to an exact "copy" of this type. And for some reason I needed
to avoid to have the type in a separate shared assembly.



I've red that DataContractSerializer class you talked about use XML.But I'm
still with Framework 2.0 and Vs2005, so I decided to try keep my code, and
change only the serialization part from Binary to Xml.. and that worked fine
!!



Strange that MS make it works using Xml Serializer/Deserializer and not with
Binay Serializer/Deserializer...



Thanks again for your help

!

Steph.




Nicholas Paldino said:
Steph,

Do you have a reference in the other application to the TestStruct
structure, or did you copy the code? If you copied the code, then in
essence, you have created a NEW type. It has the same logical structure but
it is a different type to the CLR.

If you are using .NET 3.0, then you can use the DataContract attribute
along with the DataContractSerializer class to write your files and read
them. This will allow you to have separate CLR types which have the same
logical structure and read them from the file.

You can also achieve the same thing with serialization in .NET 2.0 using
serialization surrogates, but it's pretty messy.

Also, for this kind of structure, you could get away with Xml
Serialization as well, which works in .NET 2.0 and earlier.

Of course, if you take the type and store it in an assembly that is
referenced by both executables, then all of this is moot.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

TheSteph said:
Hi,

I have a small serializable struct :

[Serializable]
public struct TestStruct
{
public string Title;
public int Age;
public string Name;
public string Surname;
}

I "binary" Serialize it in a file without any problem.
I can desreialize it in the SAME application with NO problems.
But when I want to Deserialize it in an OTHER application, I get an
Invalide
cast error :

TestStruct TmpTestStruct = (TestStruct)formatter.Deserialize(fs);

=> Error message "Specified cast is not valid" !

So, I tested to deserialize it in an object, and check the Type :

Object TmpObj = formatter.Deserialize(fs);
MessageBox.Show(TmpObj.GetType().ToString());

It works fine, and the MessageBox show "TestStruct". but impossible to
cast
it as TestStruct !! ("Specified cast is not valid")

So why is it deserializable in the same Application and not in an other,
even if both use the same source ??

Can anybody help me ? Any idea of what I am doing wrong ? a MS Bug ? Is
it
a problem because of different namesapce in both applications ?

Any help appreciated.

Steph.
 
N

Nicholas Paldino [.NET/C# MVP]

Steph,

The XmlSerializer works on logical types, not the actual CLR type. If
the layout of your type is the same, then it should work between two types.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

TheSteph said:
Many thanks for your fast, complete and instructive reply !



I actually copied the code that define my TestStruct. So the problem must
be there. I didn't thought a second that the CLR will refuse to
deserialize
from one Type to an exact "copy" of this type. And for some reason I
needed
to avoid to have the type in a separate shared assembly.



I've red that DataContractSerializer class you talked about use XML.But
I'm
still with Framework 2.0 and Vs2005, so I decided to try keep my code, and
change only the serialization part from Binary to Xml.. and that worked
fine
!!



Strange that MS make it works using Xml Serializer/Deserializer and not
with
Binay Serializer/Deserializer...



Thanks again for your help

!

Steph.




in
message news:[email protected]...
Steph,

Do you have a reference in the other application to the TestStruct
structure, or did you copy the code? If you copied the code, then in
essence, you have created a NEW type. It has the same logical structure but
it is a different type to the CLR.

If you are using .NET 3.0, then you can use the DataContract
attribute
along with the DataContractSerializer class to write your files and read
them. This will allow you to have separate CLR types which have the same
logical structure and read them from the file.

You can also achieve the same thing with serialization in .NET 2.0 using
serialization surrogates, but it's pretty messy.

Also, for this kind of structure, you could get away with Xml
Serialization as well, which works in .NET 2.0 and earlier.

Of course, if you take the type and store it in an assembly that is
referenced by both executables, then all of this is moot.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

TheSteph said:
Hi,

I have a small serializable struct :

[Serializable]
public struct TestStruct
{
public string Title;
public int Age;
public string Name;
public string Surname;
}

I "binary" Serialize it in a file without any problem.
I can desreialize it in the SAME application with NO problems.
But when I want to Deserialize it in an OTHER application, I get an
Invalide
cast error :

TestStruct TmpTestStruct = (TestStruct)formatter.Deserialize(fs);

=> Error message "Specified cast is not valid" !

So, I tested to deserialize it in an object, and check the Type :

Object TmpObj = formatter.Deserialize(fs);
MessageBox.Show(TmpObj.GetType().ToString());

It works fine, and the MessageBox show "TestStruct". but impossible to
cast
it as TestStruct !! ("Specified cast is not valid")

So why is it deserializable in the same Application and not in an
other,
even if both use the same source ??

Can anybody help me ? Any idea of what I am doing wrong ? a MS Bug ? Is
it
a problem because of different namesapce in both applications ?

Any help appreciated.

Steph.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top