In many of our programs, you need to save your data locally so that you can see the original data the next time you open it. For example, our Xmind mind map, such as our power designer, and so on, all have to save a subordinate's own project file, then today I would like to say is, the project file in the serialization and deserialization.
Introduction
Serialization is the process of storing the state of a loved object instance into a storage medium. In this procedure, the public and private fields of the object are first converted to a byte stream with the name of the class (including the assembly of the Class), and then the byte stream is written to the data stream, and the same copy of the original object is created when the object is subsequently deserialized.
"Why Serialize?" What are the benefits of serialization? 】
1. Save the State of the object in the storage media so that the exact same copy can be recreated later.
2. Send objects from one application domain to another by value. This makes it easy for the memory object to become disk data, to facilitate network transmission, for applications such as distributed, WCF, and so on (JSON is actually serializing objects into strings). Implement backup and restore of objects.
"Persistent storage"
We often need to save the object's field values to disk and retrieve the data later. Although we can do this without serialization, this approach is often cumbersome and prone to errors. When you need to track the hierarchy of objects, it becomes more and more complex. It is conceivable that if we were to write a large business application with a large number of objects, the developer would have to write code for each object to save the fields and properties to disk and restore the objects and properties from disk. Serialization provides a quick and easy way to achieve this goal.
"Serialization Kind"
One is binary serialization. Converts an object into a binary file.
Two types are XML serialization. The way an object is converted to an XML file.
The two are different from the former, which serializes all member variables (including private,protected,public) fields or properties, which only serialize public fields and properties.
"Serialization Mode"
To serialize a class, you first need to mark it as serializable ([Serializable]). Usually we serialize in two ways, one is automatic serialization, just add the [Serializable] attribute tag above the class name header. The other is manual serialization, which requires the addition of the [Serializable] attribute tag and implements the ISerializable interface.
The advantage of automatic serialization is that we can artificially serialize some fields or properties. Of course, this field or property is not serialized as long as we do not need to serialize the field or attribute tag [nonserialized] in the automatic serialization process.
Automatic serialization, the system is using "<filename>k__backingfield"
For example, the above one data modeling, in the automatic serialization
1 [Serializable] 2 public class person 3 { 4 public int Age {get ; set ;} 5 Public string Name {get ; set ;} 7 }
In an automatic serialization file, there is a form like this:
Manual serialization
1 [Serializable]2 Public classanimal:iserializable3 {4 Public intAge {Get;Set; }5 6 Public stringName {Get;Set; }7 8 PublicAnimal ()9 {Ten One } A - PrivateAnimal (SerializationInfo info, StreamingContext context) - { theAge = info. GetInt32 (" Age"); -Name = info. GetString ("Name"); - } - + Public voidGetObjectData (SerializationInfo info, StreamingContext context) - { +Info. AddValue (" Age", age); AInfo. AddValue ("Name", Name); at } -}
By comparing them in two ways, we can see that manual serialization allows us to specify a property or field to map the name on disk.
"Engineering compatibility issues"
Now that we're doing the development, it's possible to modify the previous data modeling because of the needs of the business. For example, add an attribute to the original modeling, and so on. If you add a property, the problem occurs when deserializing, causing the open project to fail. The reason is that, for example, one of the previously serialized objects contains three fields, but when deserializing, you need to deserialize four fields, which will result in a failure.
To do engineering compatibility, you first need to see whether the previous modeling was automated serialization or manual serialization. If it is automatic serialization, you need to turn automatic serialization into manual serialization, and secondly we need to determine if a property exists in it.
1 [Serializable]2 Public classanimal:iserializable3 {4 Public intAge {Get;Set; }5 6 Public stringName {Get;Set; }7 8 PublicAnimal ()9 {Ten One } A - PrivateAnimal (SerializationInfo info, StreamingContext context) - { the //Age = info. GetInt32 ("Age"); - //Name = info. GetString ("Name"); - varSerializeddata =Getserializeddata (info); - varCurrentData =Getserializabledata (); + foreach(varMemberInfoinchserializeddata) - { + varval =Memberinfo.value; A if(val = =NULL) at { - Continue; - } - if(Currentdata.containskey (memberinfo.key)) - { - Currentdata[memberinfo.key] (val); in } - } to } + - Public voidGetObjectData (SerializationInfo info, StreamingContext context) the { *Info. AddValue (" Age", age); $Info. AddValue ("Name", Name);Panax Notoginseng } - the /// <summary> + ///field or property projection in the current class A /// </summary> the /// <returns></returns> + Privatedictionary<string, action<Object>>Getserializabledata () - { $ return Newdictionary<string, action<Object>> $ { -{" Age", Value=>age = (int) value}, -{"Name",value=>value. ToString ()} the }; - }Wuyi the /// <summary> - ///in serialization, the projection of a field or property Wu /// </summary> - /// <param name= "info" ></param> About /// <returns></returns> $ Privatedictionary<string,Object>getserializeddata (SerializationInfo info) - { - varSerializs =info. GetEnumerator (); -dictionary<string,Object> dic =Newdictionary<string,Object>(); A while(Serializs. MoveNext ()) + { the DiC. ADD (Serializs. Current.name, Serializs. Current.value); - } $ returndic; the } the}
If new requirements are required to add fields or attributes in modeling, we just need to add the fields that need to be serialized in GetObjectData, and in Getserializabledata, add the processing of deserialization. This way, we can open the previous data in a new project, no matter how many fields are added.
Summary
Technology is not cattle, only the problem you have met no, deal with success did not. When one day you have enough problems to solve the problem enough, you become the person standing on the spire of the dazzling. Today's content is a bit rough, may have been before someone on the blackboard, maybe I am here a little superfluous, and the pen is a bit flowing messy, next time we will if you modify the assembly name in the program, how to deal with compatibility issues.
"Den cookbook Wild Wolf" Serializer Miaoshouhuichun