. Net serialization and deserialization (1) -- Automatic serialization

Source: Internet
Author: User

. Net methods for automatic serialization and deserialization of objects are implemented through reflection. Therefore, we can not only serialize public fields, but also serialize private fields and basic fields.

. Net stores the object state in stream in serialization. Therefore, you can serialize an object to any media by selecting an appropriate stream type. For example, files, networks, and XML. However, static fields cannot be serialized.

Serializable attributes

You can use the serializable attribute in the class definition to indicate that the class is serializable.

 

[Serializable]
Public class myclass
{
Private int ID;

Private string name;
}

Nonserialized attributes

In a serializable class, what if a member cannot be serialized? The solution is to mark the attribute of the class member as nonserialized.

 

1 [serializable]
2 public class myclass
3 {
4 private int ID;
5
6 private string name;
7
8 [nonserialized]
9 private object state;
10}

 

 

Ideserializationcallback Interface

During deserialization,. Net initializes each non-serializable member variable to the default value of this type. Then you can set the variable to the correct value in the provided method. To achieve this, we need to complete the defined processing method after deserialization. In this case, the serialization class must implement the ideserializationcallback interface. After the. NET deserialization object, the ideserializationcallback method ondeserialization () is called to complete our custom deserialization steps.

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.