Serialization and deserialization in. net

Source: Internet
Author: User
I. Overview
When two processes perform remote communication, they can send different types of data to each other. Regardless of the type of data, it is transmitted on the network in the form of binary sequence. The sender needs to convert the object to a byte sequence before it can be transmitted over the network. The receiver needs to restore the byte sequence to an object.
The process of converting an object into a byte sequence is called Object serialization.
The process of restoring a byte sequence to an object is called Object deserialization.

Ii. Object serialization has two main purposes:
1 The object's byte sequence is permanently stored on the hard disk, usually stored in a file;
We often need to save the field value of the object to the disk and retrieve the data later. Although this can be done without serialization, this method is often cumbersome and error-prone, and will become more and more complex when you need to trace the object hierarchy. Imagine writing large business applications that contain a large number of objectsProgramIn this case, programmers have to writeCodeTo save the fields and attributes to the disk and restore them from the disk. Serialization provides a quick way to easily achieve this goal. The Common Language Runtime (CLR) manages the distribution of objects in the memory. the. NET Framework uses reflection to provide an automatic serialization mechanism. After the object is serialized, the class name, assembly, and all data members of the class instance are written to the storage media. Objects usually use member variables to store references to other instances. After the class is serialized, the serialization engine tracks all serialized reference objects to ensure that the same object is not serialized multiple times .. The serialization architecture provided by the. NET Framework can automatically and correctly process object charts and circular references. The only requirement for object charts is that all objects referenced by objects being serialized must be marked as serializable (see Basic serialization ). Otherwise, an exception occurs when the serialization program attempts to serialize unlabeled objects. When deserializing A serialized class, the class is re-created and the values of all data members are automatically restored.
  2 ) Transmits the object's byte sequence over the network.
The object is valid only in the application domain of the created object. Unless the object is derived from marshalbyrefobject or marked as & nbsp; serializable, any attempt to pass the object as a parameter or return it as a result will fail. If the object is marked as & nbsp; serializable, the object will be automatically serialized, transmitted from one application domain to another application domain, and then deserialized, in this way, an exact copy of the object is generated in the second application domain. This process is usually called value-based mail. If the object is derived from marshalbyrefobject, the object reference, not the object itself, is passed from one application domain to another application domain. You can also mark the object derived from marshalbyrefobject as serializable. When this object is used remotely, the formatting program that is responsible for serialization and is pre-configured as surrogateselector controls the serialization process and replaces all objects derived from externalbyrefobject with a proxy. If it is not pre-configured as surrogateselector, the serialization architecture will follow the following standard serialization rules.

Iii.. NET provides three serialization Methods
 [1], XML serializer
[2]. Soap serializer
[3]. binaryserializer

 Iv. Basic serialization
To make a class serializable, the simplest way is to mark it with the serializable attribute, as shown below:
[Serializable]
Public class myobject
{
Public int n1 = 0;
Public int n2 = 0;
Public String STR = NULL;
}
[Binaryserializer]
Serialize an instance of this type into a file:

Myobject OBJ = New Myobject ();
OBJ. n1 = 1 ;
OBJ. n2 = 24 ;
OBJ. Str = " Some strings" ;
Iformatter formatter = New Binaryformatter ();
Stream stream = New Filestream ( "Myfile. bin" , Filemode. Create,
Fileaccess. Write, fileshare. None );
Formatter. serialize (stream, OBJ );
Stream. Close ();

 Deserialization:

Iformatter formatter = New Binaryformatter ();
Stream stream = New Filestream ( "Myfile. bin" , Filemode. Open,
Fileaccess. Read, fileshare. Read );
Myobject OBJ = (myobject) formatter. deserialize (fromstream );
Stream. Close ();

[xmlserializer]
serializes these instances into an XML file.

Xmlserializer SER =NewXmlserializer (obj. GetType ());
Ser. serialize (NewFilestream (@ "users. xml", filemode. Create), OBJ );

 Deserialization:

xmlserializer serializer = New xmlserializer (type. getType ( "myobject" );
myobject my = (myobject) serializer. deserialize (New filestream (@ "users. XML " , filemode. open);

Note: The binary formatting program is used for serialization. You only need to create an instance of the stream and formatting program to be used, and then call the serialize method of the formatting program. The stream and the object instance to be serialized are provided to this call as parameters. All member variables (or even private variables) in the class will be serialized, but this is not explicitly reflected in this example. In this regard, binary serialization is different from the XML serialization program that only serializes public fields. It is also easy to restore an object to its previous state. First, create a formatter and a stream for reading, and then let the formatter deserialize the object.
[Soap serializer]
If portability is required, use soapformatter. All you need to do is change binaryformatter in the above Code to soapformatter, while the calls of serialize and deserialize remain unchanged.

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.