What is serialization and deserialization
Serialization (serialization) is the process of converting an object or object graph (a view of an object at a specific point in time) into a byte stream. Deserialization (deserialization) is the process of swapping a byte flow back into an object graph.
Application Scenarios:
- The state of the application (object graph) can be saved to a disk file or database and resumed the next time the application runs.
- A group of objects can be easily copied to the Clipboard on a Windows Form, and then pasted back to the same or another application.
- To send an object by value from one application domain to another program domain
24.1 Quick start of serialization/deserialization
http://blog.csdn.net/gavinchengshubo/article/details/8378441
Reference System.Runtime.Serialization.Formatters.Binary
PrivateStatic MemoryStream Serializetomemory (ObjectObjectGraph) {MemoryStream stream =NewMemoryStream (); BinaryFormatter formatter =NewBinaryFormatter (); Formatter. Serialize (stream, objectgraph);ReturnStream;}PrivateStaticObject Deserializeformmemory (Stream stream) {BinaryFormatter formatter =NewBinaryFormatter ();ReturnFormatter. Deserialize (stream);}Staticvoid Main (String[] args) {list<String> objectgraph =New list<string> {"hzd" ian.w" whx1973< Span style= "color: #800000;" > "}; Stream stream = Serializetomemory (objectgraph); stream. Position = 0nullstring>foreach (var s in ObjectGraph) {Console.WriteLine (s);}}
The serializetomemory method creates a MemoryStream object that is used to create a container for the existence of a byte after serialization is completed, creating a BinaryFormatter Object formatter(formatter), calling the Serialize method, passing a reference to a Stream object and going to be serialized
The reference to the object. A Stream object can be any object of any type derived from the System.IO.Stream abstract base class (Memorystream,filestream,networksteam ), the second parameter can be anything, such as Int32, String,
List<string>, wait.
Formatter , a method that gets the stream as a parameter, returns a reference to the root object in the deserialized object graph. Internally, the formatter's
DeserializeThe method checks the contents of the stream to create an instance of all the objects in the stream, and initializes the fields of all those objects so that they have the same value as the original serialization. Usually you willDeserializeThe object reference returned by the method transforms to the type that the application expects.
Make a type serializable
Type is not serializable by default and needs to be customized System. SerializableAttribute Attribute (This attribute in System namespace), SerializableAttribute this custom attribute can only be applied to reference
types ( class ), value types ( struct ), enumeration type ( enum ) and delegate type ( delegate ). In addition SerializableAttribute attribute is not inherited by derived classes
Control of serialization and deserialization
when you apply SerializableAttribute This custom attribute to a type, all instance fields (public private protected, and so on) will be serialized. Custom attribute with System.nonserializedattribute
to indicate which fields of the type should not be serialized. This attribute is also located in the System namespace
[Serializable] class circle{ doublePrivate publicCircle (double radius) {M_radius = Radius M_area = Math.PI * M_radius * M_radius;}}
In the code above, the Circle object can be serialized, but when deserialized,
[serializable]internal class circle{private Span style= "color: #0000ff;" >double M_radius; [NonSerialized] private Double M_area; public Circle (double RADIUS) {M_radius = radius; m_area = Math.PI * M_radius * m_ Radius } [ondeserialized] private void OnDeserialized (StreamingContext context) {M_area = Math.PI * M_radius * M_radius; }}
In the modified code, there is a method of tagging with System.Runtime.Serialization.OnDeserializedAttribute custom attributes, one instance of each deserialization type, The formatter checks to see if a method that applies the attribute is defined in the type. In addition to ondeserializedattribute this custom feature, the System.Runtime.Serialization namespace also contains Ondeserializingattribute, Onserializingattribute,onserializedattribute These custom-made attribute.
24th Chapter Runtime serialization