When the serializable feature is used, if the class is updated (but must be compatible with the previously serialized Class Object), you can use the optionalfield feature to mark the newly added field. As for the serializable feature in class inheritance, the problem is not very big. As long as the parent class is marked with the serializable feature, the Child class can serialize the content of the parent class.
For the iserializable interface, because the serialization and deserialization have to be done by yourself, it is a little complicated to handle class update and inheritance issues.
For example, for version update, we can add our own version information to serializationinfo, and perform different operations based on different version information in deserialization.
The following is a type that inherits iserializable:
// + Using system. runtime. serialization;
[Serializable]
Class A: iserializable
{
Int data;
// Serialization
Public void getobjectdata (serializationinfo info, streamingcontext context)
{
Info. addvalue ("_ version", 1 );
Info. addvalue ("data", data );
}
// Deserialization
Protected A (serializationinfo info, streamingcontext context)
{
Int ver = info. getint32 ("_ version ");
If (Ver = 1)
Data = info. getint32 ("data ");
Else
Throw new serializationexception ("unsupported version ");
}
}
If a adds a new field, perform different operations on serialization and deserialization based on different versions:
// + Using system. runtime. serialization;
// Updated
[Serializable]
Class A: iserializable
{
Int data;
// Newly added Field
Int data_ver2;
// Serialization
Public void getobjectdata (serializationinfo info, streamingcontext context)
{
Info. addvalue ("_ version", 2 );
Info. addvalue ("data", data );
Info. addvalue ("data_ver2", data_ver2 );
}
// Deserialization
Protected A (serializationinfo info, streamingcontext context)
{
Int ver = info. getint32 ("_ version ");
// Process Old Version Content
If (ver> = 1)
Data = info. getint32 ("data ");
// Process new fields
Else if (ver> = 2)
Data_ver2 = info. getint32 ("data_ver2 ");
Else
Throw new serializationexception ("unsupported version ");
}
}
For an inherited class, note that the operation method of the parent class must be visible to the inherited class (public or protected), and then the inherited class can call the method of the parent class.
Code:
// + Using system. runtime. serialization;
// Parent class
[Serializable]
Class bcls: iserializable
{
Int ID;
// Serialization
Public Virtual void getobjectdata (serializationinfo info, streamingcontext context)
{
Info. addvalue ("ID", ID );
}
// Deserialization
Protected bcls (serializationinfo info, streamingcontext context)
{
Id = info. getint32 ("ID ");
}
}
// Inheritance class
[Serializable]
Class DClS: bcls, iserializable
{
String newdata;
Public override void getobjectdata (serializationinfo info, streamingcontext context)
{
// Call the parent class getobjectdata
Base. getobjectdata (Info, context );
Info. addvalue ("newdata", newdata );
}
// Call the parent class deserialization Constructor
Protected DClS (serializationinfo, streamingcontext context)
: Base (Info, context)
{
Newdata = info. getstring ("newdata ");
}
}