serialization " can be defined as the process of storing the object state to the storage media. In this process, the public and private fields of the object and the name of the class (including the Program set containing the class) are converted to byte streams and then written to the data stream. When you deserialize this object in the future, create an exact copy of the original object.
1. Why is serialization selected?
one reason is to keep the object state in the storage media, this allows you to recreate an exact copy later.
another reason is that an object is sent from one application domain to another by using a value.
for example, serialization can be used in Asp.. Net to save the session Status and copy the object to the clipboard of the Windows form. Remote processing can also use serialization to pass objects from one application domain to another through values.
2. How to serialize and deserialize objects
to serialize objects, make sure 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 serialize a class is to add the serializable attribute tag class. For example,
[serializable ()]
public class meablock
{< br> private int m_id;
Public String Caption;
Public meablock ()
{
/// Constructor
}
}
This class can be serialized.
To serialize an instance of this class to a file?. NET FrameworkTwo methods are provided:
1,XMLSerialization
UseXmlserializerClass to serialize the following items.
- Public read/write attributes and fields of public classes
- ImplementationIcollectionOrIenumerable. (Note that only the set will be serialized, but the public attribute will not .)
- XmlelementObject.
- XmlnodeObject.
- DatasetObject.
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 Public Private fields are not saved.
Generated XML The 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)
2 , Binary serialization
And XML The difference in serialization is that binary serialization can serialize all fields (including 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 = new filestream ("myfile. bin ", filemode. open, fileaccess. read, fileshare. read);
meablock myblock = (meablock) formatter. deserialize (Stream);
stream. close ();
3. How to serialize and deserialize custom visual controls in disguise
for custom controls in winform , because it inherits from system. windows. form class, And the form class is inherited from externalbyrefobject , the form itself cannot be serialized, the form implementation is based on GUI resources under Win32 , and cannot be exists from the current context.
you can use a flexible method to serialize controls. The memory model is used here.
defines the memory class (in fact, it is a serializable object class) used to record the valid attributes of the Control. When the control needs to be serialized, you only need to copy the instance of the control to the memory class to serialize and 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.