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.