Information Technology Reference
In-Depth Information
// Overridden in derived classes to write
// derived class data:
protected virtual void
WriteObjectData(
SerializationInfo inf,
StreamingContext cxt)
{
// Should be an abstract method,
// if MyType should be an abstract class.
}
}
A derived class would provide its own serialization constructor and over-
ride the WriteObjectData method:
public class DerivedType : MyType
{
private int derivedVal;
private DerivedType( SerializationInfo info,
StreamingContext cntxt) :
base (info, cntxt)
{
derivedVal = info.GetInt32( "_DerivedVal" );
}
protected override void WriteObjectData(
SerializationInfo inf,
StreamingContext cxt)
{
inf.AddValue( "_DerivedVal" , derivedVal);
}
}
The order of writing and retrieving values from the serialization stream
must be consistent. I've chosen to read and write the base class values first
because I believe it is simpler. If your read and write code does not serial-
ize the entire hierarchy in the exact same order, your serialization code
won't work.
None of the code samples in this item use automatic properties. That's by
design. Automatic properties use a compiler-generated backing field for
 
Search WWH ::




Custom Search