473,322 Members | 1,347 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,322 software developers and data experts.

Strange Deserialization problem.

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
3 2195
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: TEK | last post by:
There seems to be a bug when deserialization some classes in the .NET framework. If you try to deserialize a class that has a base class that holds a struct with a member that is implementing...
2
by: Snowman | last post by:
Suppose I have a RootObject which holds a collection of other objects. The other objects have a property (Parent) which refers back to the "parent" collection (b.t.w. my collection is based on...
3
by: Amadelle | last post by:
Hi all and thanks in advance for your help, I am having problems deserializing an object which seems to be serializing just fine. I save the byte array of the serialized object in the database...
3
by: AnkitAsDeveloper [Ankit] | last post by:
Hi i am serializing a 'ref struct' object as follows : private: void Seri( String ^path, Object^ obj ) { FileStream^ fileStrm ; try { //Serialize entire object into Binary stream
3
by: parrot toes | last post by:
Summary: I have been trying to make requests of a web service provided by Axis using a dotnet client with code generated by wsdl.exe and have been getting exceptions when trying to process the...
1
by: parrot toes | last post by:
I tried to post this question before, but there was an error when posting. I case it did get posted and in order to avoid duplication, I'll just repost a summary. I have written a dotnet client...
5
by: Greg Allen | last post by:
I am consuming a web service and using the generated Reference.cs to access the service and the objects associated with it. I have run into a problem where some inherited classes are not being...
3
by: John Glover | last post by:
To whoever can help, I've been having a problem with XML deserialization lately. I needed to serialize and deserialze two objects which inherited from Hashtable. Because IDictionary...
7
by: Andrew | last post by:
Hi, I am using DataContractJsonSerializer to deserialize JSON string in C# objects but I am having a problem. Suppose I have a class: class Item { public ItemId Id { get; set; }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.