Serialization Issue with Int32
-
While I'm posting possible problems I've found... When a struct (or class for that matter) with an Int32 ^ is deserialized an exception is thrown. Again: Is this a "feature" that I just haven't seen documented anywhere, or an actual issue? ----CODE---- using namespace System; using namespace System::IO; using namespace System::Runtime::Serialization::Formatters::Binary; [Serializable] public ref struct TestStruct { Int32^ i; }; [Serializable] public ref struct TestStruct2 { int i; }; int main(array ^args) { //MemoryStream to do use for serializing MemoryStream^ ms = gcnew MemoryStream(); //Binary Format BinaryFormatter^ bf = gcnew BinaryFormatter(); //Create the structure TestStruct ^t = gcnew TestStruct(); //Initialize the data t->i = 42; //Serialize the string bf->Serialize(ms,t); //GO back to the beginning of the stream to deserialize ms->Seek(0,SeekOrigin::Begin); try { Object^ o = bf->Deserialize(ms); System::Console::WriteLine("o is {0}",o); } catch(System::Runtime::Serialization::SerializationException^ e) { System::Console::WriteLine("Caught an exception while trying to deserialize TestStruct!"); System::Console::WriteLine(e->Message); } /* Generates Exception: Type: System.Runtime.Serialization.SerializationException Additional information: Binary stream '42' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization. Note the 42. If t->i is set to a different value, this changes. */ //Reset for second test ms = gcnew MemoryStream(); bf = gcnew BinaryFormatter(); TestStruct2 ^t2 = gcnew TestStruct2(); t2->i = 42; bf->Serialize(ms,t2); ms->Seek(0,SeekOrigin::Begin); try { Object^ o = bf->Deserialize(ms); System::Console::WriteLine("o is {0}",o); } catch(System::Runtime::Serialization::SerializationException^ e) { System::Console::WriteLine("Caught an exception while trying to deserialize TestStruct2!"); System::Console::WriteLine(e->Message); } /* No Exception Generated */ return 0; } ----CODE END----
-
While I'm posting possible problems I've found... When a struct (or class for that matter) with an Int32 ^ is deserialized an exception is thrown. Again: Is this a "feature" that I just haven't seen documented anywhere, or an actual issue? ----CODE---- using namespace System; using namespace System::IO; using namespace System::Runtime::Serialization::Formatters::Binary; [Serializable] public ref struct TestStruct { Int32^ i; }; [Serializable] public ref struct TestStruct2 { int i; }; int main(array ^args) { //MemoryStream to do use for serializing MemoryStream^ ms = gcnew MemoryStream(); //Binary Format BinaryFormatter^ bf = gcnew BinaryFormatter(); //Create the structure TestStruct ^t = gcnew TestStruct(); //Initialize the data t->i = 42; //Serialize the string bf->Serialize(ms,t); //GO back to the beginning of the stream to deserialize ms->Seek(0,SeekOrigin::Begin); try { Object^ o = bf->Deserialize(ms); System::Console::WriteLine("o is {0}",o); } catch(System::Runtime::Serialization::SerializationException^ e) { System::Console::WriteLine("Caught an exception while trying to deserialize TestStruct!"); System::Console::WriteLine(e->Message); } /* Generates Exception: Type: System.Runtime.Serialization.SerializationException Additional information: Binary stream '42' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization. Note the 42. If t->i is set to a different value, this changes. */ //Reset for second test ms = gcnew MemoryStream(); bf = gcnew BinaryFormatter(); TestStruct2 ^t2 = gcnew TestStruct2(); t2->i = 42; bf->Serialize(ms,t2); ms->Seek(0,SeekOrigin::Begin); try { Object^ o = bf->Deserialize(ms); System::Console::WriteLine("o is {0}",o); } catch(System::Runtime::Serialization::SerializationException^ e) { System::Console::WriteLine("Caught an exception while trying to deserialize TestStruct2!"); System::Console::WriteLine(e->Message); } /* No Exception Generated */ return 0; } ----CODE END----
I think you need to change the defintion of TestStruct i from Int32^ i; to Int32 i; for this to work. gmileka
-
I think you need to change the defintion of TestStruct i from Int32^ i; to Int32 i; for this to work. gmileka
Yes, while I agree that that does work, what I really want to know is WHY cant I use Int32^ when things like String^ work just fine. I think there should be some documentation somehwere that says "You can't serialize certain references.. " somewhere. Myabe there already is, but i can't find it.