Serialization is the process of converting an object's state to a format that can be persisted or transmitted. The opposite of serialization is deserialization, which converts a stream to an object. These two processes combine to make it easy to store and transfer data.
Several serialization techniques:
1 binary serialization preserves type fidelity, which is useful for preserving the state of an object between different invocations of an application. For example, you can share objects between different applications by serializing the object to the Clipboard. You can serialize objects to streams, disks, memory, and networks, and so on. Remoting uses serialization "by value" to pass an object between a computer or an application domain.
2 XML serialization only serializes public properties and fields, and does not maintain type fidelity. This is useful when you want to provide or use data without restricting applications that use that data. Because XML is an open standard, this is a good choice for sharing data over the WEB. SOAP is also an open standard, making it an attractive option.
3 The type instance is serialized and deserialized into an XML stream or document (or JSON format) using the provided data contract. Often applies to WCF communications.
BinaryFormatter
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 the object is deserialized later, an exact copy of the original object is created.
1, the simplest way to make a class serializable is to use the Serializable property tag as follows.
2. Selective serialization
By labeling member variables with the NonSerialized property, you can prevent them from being serialized
3, custom serialization
1 To run the custom method during and after serialization
Best practices are also the simplest approach (introduced in the. Net Framework version 2.0), which is to apply the following attributes to the method used to correct the data during and after serialization:
Ondeserializedattribute
Ondeserializingattribute
Onserializedattribute
Onserializi Ngattribute
Specific examples are as follows:
This is the object of would be serialized and deserialized. [Serializable ()] public class Testsimpleobject {//This's serialized and deserialized with no Chang
E. public int member1;
The value of this field is set and reset during and//after serialization.
private string Member2; This field is not serialized.
The Ondeserializedattribute//is used to set the ' member value ' after serialization.
[NonSerialized ()] public string Member3;
This field was set to null, but populated after deserialization.
private string Member4;
Constructor for the class.
Public Testsimpleobject () {member1 = 11;
Member2 = "Hello world!";
Member3 = "This is a nonserialized value";
Member4 = null;
public void Print () {Console.WriteLine ("Member1 = ' {0} '", member1);
Console.WriteLine ("Member2 = ' {0} '", member2); Console.writEline ("Member3 = ' {0} '", Member3);
Console.WriteLine ("Member4 = ' {0} '", member4); [OnSerializing ()] internal void Onserializingmethod (StreamingContext context) {member2 = ' this
Value went into the data file during serialization. ";} [OnSerialized ()] internal void Onserializedmethod (StreamingContext context) {member2 = ' This value is re
Set after serialization. ";} [OnDeserializing ()] internal void Ondeserializingmethod (StreamingContext context) {Member3 = ' this value
was set during deserialization "; [OnDeserialized ()] internal void Ondeserializedmethod (StreamingContext context) {member4 = ' Th Is value ' set after deserialization. ';}}