Serializing object problem
-
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 -
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örgHi 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 aFixup
method that must be called after deserialization. ---------------------------- Be excellent to each other :) EasiReports[^] My free reporting component for WinForms. -
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 aFixup
method that must be called after deserialization. ---------------------------- Be excellent to each other :) EasiReports[^] My free reporting component for WinForms.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