. Net (C #): About iserializable serialization and class update and inheritance

Source: Internet
Author: User
Tags unsupported

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 ");

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.