Version compatibility issues in. Net serialization

Source: Internet
Author: User
Tags net serialization object serialization

If you try to deserialize an earlier version of the object, there may be version compatibility issues. For example, if you add a member to your custom class, when you deserialize earlier versions, an exception (. net framework1.0 or 1.1 ). in this case, there are usually two solutions:

1. Import the custom classes of the old version when serializing the new version.

2. Identify the newly added member as optionalfield.

The optionalfied attribute does not affect the object serialization process. When the CLR encounters the optionalfiled attribute during deserialization, it automatically assigns a null value to the modified member instead of throwing an exception. For example:

[Serializable]
Class Shoppingcartitem: ideserializationcallback
{
Public   Int Productid;
Public   Decimal Price;
Public   Int Quantity;
[Nonserialized]
Public   Decimal Total;
[Optionalfield]
Public   Bool Taxable;
}

 

Therefore, we recommend that you:

1. Do not remove fields with [serialized] attributes.

2. do not identify the [nonserilized] attribute to the field. If the field is serialized in an earlier version

3. Do not change the member type or name.

4. When a new serialized member is added, it must be identified as [optionalfield].

5. If you want to remove the [nonserilized] attribute, we recommend that you use [optionalfield] instead.

6. if you want to initialize the [optionalfield] field in deserialization, You can implement the ideserializationcallback interface and ondeserialization method. unless you only need to assign 0 or null values.

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.