In order to transmit data, we need to serialize the entity class as the carrier, so we have a good introduction to serialization. I feel it is easy to introduce the following introduction!
1. What is serialization?
Serialization is the process of converting the object state to a format that can be maintained or transmitted. During the serialization process, the public and private fields of the object and the name of the class (including the Assembly containing the class) are converted to byte streams and then written to the data stream. In contrast to serialization, deserialization converts a stream into an object. These two processes can be combined to easily store and transmit data.
2. Why serialization?
A. One reason is to keep the object state in the storage media so that you can recreate an exact copy later.
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 a large business application that contains a large number of objects. programmers have to write code for each object to save fields and attributes to the disk and restore these fields and attributes from the disk. Serialization provides a quick way to easily achieve this goal.
B. Another reason is that an object is sent from one application domain to another by using a value.
For example, serialization can be used to save the session state in ASP. NET and copy the object to the clipboard of Windows Forms. Remote processing can also use serialization to pass objects from one application domain to another through values.
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.
3. How to Implement Object serialization and deserialization
To serialize an object, ensure that the object can be serialized. In addition, serialization only saves the object attributes effectively, but some methods of the object cannot be serialized.
The easiest way to implement serializable class serialization is to add the serializable attribute tag class. For example:
[Serializable ()]
Public class meablock
{
Private int m_id;
Public String Caption;
Public meablock ()
{
/// Constructor
}
}
This class can be serialized. Note that the serialized class must be public, otherwise it cannot be serialized.
To serialize the instance of this class to a file ?. Net Framework provides two methods:
A. XML serialization
Use the xmlserializer class to serialize the following items.
Public read/write attributes and fields of public classes
Class that implements icollection or ienumerable. (Note that only the set will be serialized, but the public attribute will not .)
Xmlelement object.
Xmlnode object.
DataSet object.
To serialize instances of the above classes, refer to the following example:
Meablock myblock = new meablock ();
// Insert code to set properties and fields of the object.
Xmlserializer myserializer = new xmlserializer (typeof (meablock ));
// To write to a file, create a streamwriter object.
Streamwriter mywriter = new streamwriter ("myfilename. xml ");
Myserializer. serialize (mywriter, meablock );
Note that XML serialization only saves public fields and does not save private fields.
The generated XML file format is as follows:
<Meablock>
<Caption> test </caption>
</Meablock>
The deserialization of objects is as follows:
Meablock myblock;
// Constructs an instance of the xmlserializer with the type
// Of object that is being deserialized.
Xmlserializer myserializer = new xmlserializer (typeof (meablock ));
// To read the file, creates a filestream.
Filestream myfilestream = new filestream ("myfilename. xml", filemode. Open );
// Callthe deserialize method and casts to the object type.
Myblock = (meablock) myserializer. deserialize (myfilestream)
B. binary serialization
Different from XML serialization, binary serialization can serialize all fields (both private and public) in the instance of the class. This makes it easier and more accurate to restore copies of objects.
To serialize instances of the above classes, refer to the following example:
Meablock myblock = new meablock ();
// Insert code to set properties and fields of the object.
Iformatter formatter = new binaryformatter ();
Stream stream = new filestream ("myfile. bin", filemode. Create, fileaccess. Write, fileshare. None );
Formatter. serialize (stream, myblock );
Stream. Close ();
The deserialization of objects is as follows:
Iformatter formatter = new binaryformatter ();
Stream stream = new filestream ("myfile. bin", filemode. Open, fileaccess. Read, fileshare. Read );
Meablock myblock = (meablock) formatter. deserialize (Stream );
Stream. Close ();
4. How to serialize and deserialize custom visual controls in disguise
Custom Controls in winform are inherited from system. windows. form class, while form class is inherited from marshalbyrefobject, the form itself cannot be serialized, the form implementation is based on the Win32 GUI resources, cannot be separated from the current context.
Of course, you can use a work und to serialize controls. The memory model is used here.
The definition memory class (actually a serializable entity class) is used to record the valid properties of the Control. to serialize the control, you only need to copy the instance of the control to the memory class, it is serialized to save the memory class.
Deserialization is an inverse process. Deserializes data streams into the memory class, and generates control instances based on the attributes of the memory class. You can continue to use some events and methods of controls.
WWF emphasizes class instantiation because the workflow and application are in different threads. To use a class as the carrier for data transmission between the two, the class must be defined as public serialization for binary transmission.