By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,996 Members | 1,051 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,996 IT Pros & Developers. It's quick & easy.

Strange Deserialization problem.

P: n/a
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.
Apr 25 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"TheSteph" <Th******@NoSpam.comwrote in message
news:ey**************@TK2MSFTNGP03.phx.gbl...
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.


Apr 25 '07 #2

P: n/a
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:OL**************@TK2MSFTNGP03.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"TheSteph" <Th******@NoSpam.comwrote in message
news:ey**************@TK2MSFTNGP03.phx.gbl...
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.


Apr 25 '07 #3

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"TheSteph" <Th******@NoSpam.comwrote in message
news:uK**************@TK2MSFTNGP03.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in
message news:OL**************@TK2MSFTNGP03.phx.gbl...
>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]
- mv*@spam.guard.caspershouse.com

"TheSteph" <Th******@NoSpam.comwrote in message
news:ey**************@TK2MSFTNGP03.phx.gbl...
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.




Apr 25 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.