Overview:
Serialization refers to the process of converting the current state of an object into a stream of bytes, and 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 bytes can be stored in a file, database, or memory, as long as it can be stored anywhere. To restore an object, you only need to deserialize it from the location it stores. This inherent property of an object is very important to Stateless web applications because it allows important state 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 .. NET remote framework uses serialization to group object data among processes.
Serialization is not. NET introduces new concepts, Windows programmers deal with a variety of serializing: the archiving mechanism in MFC, The propertybag of visuabl basic and the ipersiststream interface of COM all have similar features. So what is the difference between. Net serialization and these methods? A word, simple. In. in the previous development environment of net, serialization is a manual customization. For Class designers, it is not difficult to implement serialization of classes, but it is difficult to implement the design, it seems troublesome and time-consuming. In. net, this process is completely automated. What the user does is to mark a serializable class using the serializable feature, and the Runtime Library does the rest.
How can the. NET Runtime Library achieve this shortcut? Of course, the image is used, or reflection is more accurate. As mentioned above, the Assembly contains self-describing data named metadata. The Assembly metadata fully expresses each internal type, including the information of each private field of the type. Through the reflection process, the runtime database can identify metadata and use it to determine how classes should be serialized.
Serializable:
As mentioned above, if the Runtime Library is allowed to serialize an object, the class must be marked with the serializable feature. The following code defines two classes that mark the serialable feature.
[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, the mnickname field of the car class is marked with the nonserialized feature, indicating that this item is skipped during serialization of the runtime, while other parts allow serialization. Note that mradio is 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 how to serialize a car object to 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 .. Net Framework has two formatting programs: Soap formatting program and binary formatting program. In the previous example, the soapformatter object serializes an object into a soap packet and sends it to a specific stream. Figure 2-26 shows the content in car.txt after the code is executed.
If you carefully look at Figure 2-26, it is easy to find out how soap depicts 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 calling the soapformatter. dewerialize method, you can read this object into the memory:
Static void rehydrate ()
{
Filestream mysoapfile = file. Open ("car.txt", filemode. Open );
Car mycar = (CAR) New soapformatter (). deserialize (mysoapfile );
Mysoapfile. Close ();
}
Figure 2-26 serialize the car object using the soap formatter