About. Some knowledge of serialization in net

Source: Internet
Author: User
Serialization can be defined as the process of storing the state of an object in a storage medium. In this procedure, both the public and private fields of the object and the name of the class, including the assembly that contains the class, are converted to a byte stream and then written to the data flow. When you deserialize the object later, you create an exact copy of the original object.
One, why to choose serialization
One reason is to keep the state of the object in storage media so that you can re-create the exact copy at a later time;
Another reason is to send an object from one application domain to another by value.
For example, serialization can be used to save session state in ASP.net and copy objects to a Windows forms Clipboard. Remoting can also use serialization to pass an object from one application domain to another by using a value.
Ii. How to implement the serialization and deserialization of objects
To implement serialization of an object, first make sure that the object can be serialized. Furthermore, serialization simply saves the properties of an object effectively, and it cannot be serialized for some methods of the object.
The easiest way to implement a class serializable is to add the serializable attribute tag class. Such as:
[Serializable ()]
public class Meablock
{
private int m_id;
public string Caption;

Public Meablock ()
{
Constructors
}
}
The serialization of the class can be implemented.
To serialize an instance of the class to a file? The. NET Framework provides two methods:
1. XML serialization
Using the XmLSerializer class, you can serialize the following items.
    • Public read/write properties and fields for public classes
    • A class that implements ICollection or IEnumerable. (Note that only collections are serialized and public properties are not.) )
    • XmlElement object.
    • XmlNode object.
    • The DataSet object.

To implement serialization of instances of the above class, 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);
It is to be noted that XML serialization saves only the fields that are public, not the private fields.
The resulting XML file format is as follows:
<MEABlock>
<Caption>Test</Caption>
</MEABlock>
For deserialization of an object, the following are:
Meablock Myblock;
Constructs an instance of the XmlSerializer with the type
The object is being deserialized.
XmlSerializer myserializer = new XmlSerializer (typeof (Meablock));
To read the file, creates a FileStream.
FileStream myFileStream = new FileStream ("Myfilename.xml", FileMode.Open);
Calls the deserialize method and casts to the object type.
Myblock = (meablock) myserializer.deserialize (myFileStream)
(2) binary serialization
Unlike XML serialization, binary serialization can serialize all fields (both private and public) in an instance of a class. This makes it easier and more accurate to restore the copy of the object.
To implement serialization of instances of the above class, 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 ();
For deserialization of an object, the following are:
IFormatter formatter = new BinaryFormatter ();
Stream stream = new FileStream ("Myfile.bin", FileMode.Open,FileAccess.Read, FileShare.Read);
Meablock Myblock = (meablock) formatter. Deserialize (stream);
Stream. Close ();

Iii. How to implement the serialization, deserialization of a custom visual control
For custom controls in WinForm, Because it inherits from the System.Windows.Form class and the form class inherits from MarshalByRefObject, the forms themselves cannot be serialized, and the form's implementation is based on Win32 GUI resources and cannot be detached from the current context.
Of course, you can implement serialization of controls in a flexible way. The memory class model is used here.
Defining a Memory class (which is actually a serializable entity class) is used to record a valid property of a control, and you need to serialize the control by simply copy the instance of the control to the memory class and turn it into a serialized operation that holds the memory class.
Deserialization is an inverse process. Deserializes the data stream into the memory class, and then generates the control instance based on the memory class's properties. Some events and methods for the control can continue to be used.




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.