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. C#
  4. Serializing object problem

Serializing object problem

Scheduled Pinned Locked Moved C#
data-structuresjsonhelpquestion
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.
  • H Offline
    H Offline
    hpetriffer
    wrote on last edited by
    #1

    Hello I have a strange problem. I have implemented the ISerializable interface for 2 classes. the GetObjectData implementation of the first class is: public void GetObjectData(SerializationInfo info, StreamingContext context) { List lstRegister = new List(); info.AddValue("Text", this.Text); foreach (Control c in this.Controls) { Register reg = c as Register; if (reg != null) { lstRegister.Add(reg); } } Register[] registers = lstRegister.ToArray(); info.AddValue("Registers", registers); } the serialization constructor is the following: protected VisualTabPage(SerializationInfo info, StreamingContext ctxt) : this() { this.Text = info.GetString("Text"); Register[] registers = (Register[])info.GetValue("Registers", typeof(Register[])); this.Controls.AddRange(registers); } for the Register class I have implemented also this interface and it was working well: I have tested it with the following helper class: [Serializable] internal class SerializeTab { private string name; public List visObjects = new List(); public string Name { get { return name; } set { name = value; } } } Now if I Deserialize the object, there is a Register[] array with one item, but this item is null. Has anyone an idea what can cause this? Regards Hansjörg

    N 1 Reply Last reply
    0
    • H hpetriffer

      Hello I have a strange problem. I have implemented the ISerializable interface for 2 classes. the GetObjectData implementation of the first class is: public void GetObjectData(SerializationInfo info, StreamingContext context) { List lstRegister = new List(); info.AddValue("Text", this.Text); foreach (Control c in this.Controls) { Register reg = c as Register; if (reg != null) { lstRegister.Add(reg); } } Register[] registers = lstRegister.ToArray(); info.AddValue("Registers", registers); } the serialization constructor is the following: protected VisualTabPage(SerializationInfo info, StreamingContext ctxt) : this() { this.Text = info.GetString("Text"); Register[] registers = (Register[])info.GetValue("Registers", typeof(Register[])); this.Controls.AddRange(registers); } for the Register class I have implemented also this interface and it was working well: I have tested it with the following helper class: [Serializable] internal class SerializeTab { private string name; public List visObjects = new List(); public string Name { get { return name; } set { name = value; } } } Now if I Deserialize the object, there is a Register[] array with one item, but this item is null. Has anyone an idea what can cause this? Regards Hansjörg

      N Offline
      N Offline
      Nicholas Butler
      wrote on last edited by
      #2

      Hi This might be to do with the way .Net serializes complex types - it doesn't do it all at once. Save the array to a private member field in the serialization constructor, then have a look at this when the whole serialization process has completed. If this works, you will either have to implement IDeserializationCallback, or possibly even a Fixup method that must be called after deserialization. ---------------------------- Be excellent to each other :) EasiReports[^] My free reporting component for WinForms.

      H 1 Reply Last reply
      0
      • N Nicholas Butler

        Hi This might be to do with the way .Net serializes complex types - it doesn't do it all at once. Save the array to a private member field in the serialization constructor, then have a look at this when the whole serialization process has completed. If this works, you will either have to implement IDeserializationCallback, or possibly even a Fixup method that must be called after deserialization. ---------------------------- Be excellent to each other :) EasiReports[^] My free reporting component for WinForms.

        H Offline
        H Offline
        hpetriffer
        wrote on last edited by
        #3

        I have tested this also. it doesn't work. At the moment I have made an workarround in serializing the array as Register0, Register1 ... It is not a good solution, but it works. I hope to remove this workaround in the next days, weeks... Regards Hansjörg

        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