Also, stringized

Source: Internet
Author: User
Overview:

Serializing(Serialization)It refers to converting the current state of an object into word throttling.(A stream of bytes)And deserialization.(Deserialization)It refers to the inverse process of the serialization process, which converts the byte stream into an object. It may not seem very interesting at first, but there are many important reasons for using serialization. Once an object is serialized, the obtained bytes can be stored in files, databases, or memory.--It can be stored anywhere. To restore an object, you only need to deserialize it from the location it stores. This inherent property of objects for statelessWebApplicationProgramIs very important because it allows important status information to be retained between user requests.

Serialization also allows objects to be transmitted between application boundaries. programmers can transfer byte streams describing the object state over the network and form a matched object through reverse string lines at the other end. In essence, serialization allows objects to be passed to another application in the form of "values.. NetRemote frameworkThe serialization is widely used between processes to group object data.

Serialization is not. NetNew concepts,WindowsProgrammers deal with a variety of serializing:MFCArchiving mechanism,Visuabl basicOfPropertybagAndComOfIpersiststreamInterfaces. So. NetWhat is the difference between serialization and these methods? A word, simple. In. NetIn the previous development environment, serialization was a manual customization. For Class designers, serializing classes is not difficult, but the implementation is difficult, it seems troublesome and time-consuming. In. NetThe process is completely automated. What the user does is to useSerializableFeature to mark a serializable class, And the Runtime Library does the rest.

. NetHow can the Runtime Library achieve this shortcut? Of course, the image is used, or reflection is more accurate. As mentioned above, an Assembly contains a self-description named metadata.(Self-describing)Data, the Assembly metadata completely expresses each internal type, including the information of each private field in the type. Through the reflection process, the runtime database can identify metadata and use it to determine how classes should be serialized.

UseSerializableFeatures are serialized:
As mentioned above, if the Runtime Library is allowed to serialize an object, the class must be marked
SerializableFeatures. The followingCodeTwo tags are defined.SerialableFeature class.

[Serializable]

Public class car

{

Private string mcolor;

Private int mtopspeed;

// Reference to another object

Private radio mradio;

[Nonserialized] // runtime will not serialize this field

Private string mnickname;

Public Car (string nickname, int topspeed, string color)

{

Mnickname = nickname;

Mtopspeed = topspeed;

Mcolor = color;

Mradio = new radio ();

}

}

[Serializable]

Public class radio

{

Private int mvolume = 5;

}

As shown above,CarClassMnicknameFieldNonserializedFeature annotation that indicates that this item is skipped when the runtime database is serialized, while other parts allow serialization. Note,MradioIs a reference to another object, meaning that the runtime must serialize it. In fact, when the Runtime Library is told to serialize an object, it uses the given object as the root to create an object chart.(Similar to the Garbage Collector). All objects in the chart must also be serializable. Otherwise, the runtime will cause an exception.

The following code illustrates howCarObjects are serialized into a file.

Static void main (string [] ARGs)

{

Vehicle mycar = new car ("Christine", 150, "Red ");

Filestream mysoapfile = file. Create ("car.txt ");

// Use a soap formatter object to serialize the object.

New soapformatter (). serialize (mysoapfile, mycar );

Mysoapfile. Close ();

}

The runtime database does not control the actual format of serialized data, but there is an object specially formatted to format the serialized output.. NetThe Framework has two formatting programs:SoapFormatting and binary formatting. In the previous example,SoapformatterObjects are serialized into oneSoapAnd send it to a specific stream. Figure2-26After the code is executedCar.txt.

If you carefully view the chart2-26, It is easy to findSoapIs how to depict each object and its data members. In this way, you can increase the file size to improve readability. The binary formatting program uses a more compact format. If you want to send objects between networks, this method is more important, especially when you need to consider network bandwidth without emphasizing readability.

By callingSoapformatter. dewerializeTo read the object into the memory:

Static void rehydrate ()

{

Filestream mysoapfile = file. Open ("car.txt", filemode. Open );

Car mycar = (CAR) New soapformatter (). deserialize (mysoapfile );

Mysoapfile. Close ();

}

Figure2-26UseSoapFormatting ProgramCarSerializing objects

Note: from net distributed programming-C #

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.