Reading directory
- Introduction
- Details
- Processing
- Conclusion
- Download Demo
Introduction
Serialization is the process of converting the object state into a form of persistence or transmission. The serialized supplement is deserialization, and the latter switches the stream into an object. Both processes ensure that data is easy to store and transmit.
. NET Framework provides two serialization technologies:
Binary serialization maintains type fidelity, which is useful for retaining the object state when applications are called for multiple times. For example, objects can be shared between different applications by serializing objects to the clipboard. You can serialize an object to a stream, disk, memory, or network. Serialization is used for remote processing. objects are transmitted between computer or application domains by value.
XML serialization only serializes public attributes and fields, and does not maintain type fidelity. This is useful when you want to provide or use data without limits on applications that use the data. Because XML is an open standard, it is an ideal choice for sharing data through the Web. SOAP is also an open standard, which makes it an ideal choice.
Details
Specific namespace:
- System. Runtime. Serialization
-
Contains classes that can be used to serialize and deserialize objects. (Including System. Runtime. Serialization. Formatters. Binary. BinaryFormatter, and DataContractAttribute and DataMemberAttribute used in WCF)
- System. Xml. Serialization
-
Contains classes that can be used to serialize objects to documents or streams in XML format.
You are familiar with WCF. Generally, books have a lot of content to explain the basic things. Here we will give a brick to some non-WCF serialization techniques.
I. serialization mark
The feature "Serializable" is only valid in the "class, struct, enum, delegate" statement. Therefore, it can only be marked on classes and struct.
Serialization not required: [NonSerialized] can also be written as [NonSerializedAttribute]
The feature "NonSerialized" is valid only in the "field" declaration. Therefore, you can only mark the field, not even the attribute server.
Ii. Special cases:
[Field: NonSerializedAttribute ()]. The field tag must be added.
In fact, the attribute we call is an attribute tool, which is a pair of Get and Set methods. Since it is a method, it is not a field, so it cannot be serialized or excluded. How can we handle columns without serialization for certain attributes. There are also some methods. You need to define the Get and Set methods in the attribute tool in a realistic manner, that is, the existing fields must still be defined and cannot be lazy. Then mark the fields above this field as not needing to be serialized.
Iii. Special serialization usage
When I learned C #, everyone knowsThe concept of ICloneable may also be known.This interface, this Clone interface can copy objects, such as instantiating a student, and then calling Clone () to obtain the shallow copy of the Peer line.
In the shallow clone mode, only the address of the value type and reference type of the modified object is copied.However, the objects in the original object are changed. For example, the student class has a classroom and a field in the classroom has changed, in this way, the value of the cloned classroom will change. Unless the classroom class also implements the ICloneable interface.
The advantage of serialization and cloning is that you do not need to consider shallow replication or deep replication., Directly serialize the object to be cloned, and the object obtained by deserialization is the expected result.
Processing
The Code is as follows:
Student Class
param = }
}
ExParam class
}
Util class
[] SerializeObject( (obj == (MemoryStream memory = BinaryFormatter formatter = memory.Position = [] read = memory.Read(read, DeserializeObject( obj = (data == (MemoryStream memory = memory.Position = BinaryFormatter formatter = obj = }
Main form
Form1_Load( Student student = ID = Name = Room = Name = Address = Param = Name = [] data = Student student1 = Util.DeserializeObject(data) info =.Format( , + student.GetHashCode().ToString() + , student.ID + + student.Name + , student.Room != ? student.Room.Name + + student.Room.Address + +student.Room.GetHashCode().ToString()+ : , student.Param != ? student.Param.Name : }
Conclusion
The above code output:
We can see from the output of the above Code
Download Demo
Source code download