Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Serialization Issue with Int32

Serialization Issue with Int32

Scheduled Pinned Locked Moved Managed C++/CLI
data-structuresjsonhelpquestionannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jmlstele
    wrote on last edited by
    #1

    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----

    G 1 Reply Last reply
    0
    • J jmlstele

      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----

      G Offline
      G Offline
      georgeraafat
      wrote on last edited by
      #2

      I think you need to change the defintion of TestStruct i from Int32^ i; to Int32 i; for this to work. gmileka

      J 1 Reply Last reply
      0
      • G georgeraafat

        I think you need to change the defintion of TestStruct i from Int32^ i; to Int32 i; for this to work. gmileka

        J Offline
        J Offline
        jmlstele
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups