Serialization and default construction

  • Thread starter Thread starter Herby
  • Start date Start date
H

Herby

My default constructors do not seem to be being called when
deserializing using .NET.

Consequently some of my objects that are marked [NonSerialized] are not
in a initialised state?

Whats going on here? Should not the default constructors be called?
 
Well it seems i may have solved this myself.

Your class has to implement the ISerializable interface.

And then a default constructor will be called

HerbyClass(SerializationInfo^ info, StreamingContext context)

This will then implicitly call the default constructors on any private
objects that are attributed with
[NonSerialized]

This was not obvious and definately requires a more advanced
understanding.
I would like someone to confirm this is correct?
 
Im also having problems now with above, my member classes do not seem
to be being (de)/serialized correctly
e.g.

virtual void GetObjectData(SerializationInfo^ info, StreamingContext
context){

info->AddValue("_si", SchemeInputs);
}

Where SchemeInputs are instances of my own class previously auto
serialized.


HerbyClass(SerializationInfo^ info, StreamingContext context){

ResultArray temp;

SchemeInputs = safe_cast<ResultArray^>(info->GetValue("_si",
temp.GetType()));
}
Anbody know what is wrong with above?



If class B inherits from A and has class C as a member and i implement
ISerializable for class B, does this mean i have to also implement
ISerializable for class A and C ???

Again i have found away around the problem, i have removed
ISerializable and implemented IDeserializationCallback

I have changed my private [NonSerialized] members from
HerbyClass m_c;
To
HerbyClass^ m_c;

and then dynamically create them on the invocation of

virtual void OnDeserialization(Object^ sender)


This is not that intuitive, im not convinced its correct ????
 
Back
Top