Automatically serialize the Serializable attribute to copy code 1 [Serializable] 2 public class SerializableCase 3 4 {5 6 public SerializableCase () {} 7 8 private string _ State; 9 10 public string State11 12 {13 14 get {return _ State;} 15 16 set {_ State = value ;} 17 18} 19 20} copy the code and add the Serializable attribute to the above sample type to mark the sample type as Serializable. formatting serializer binary formatter copy code 1 public class MySerializableCase 2 {3 public static void BinaryFormatterSerialize () 4 {5 IFormatter formatter = new BinaryFormatter (); 6 Stream stream = new FileStream ("jin. glory ", FileMode. create, FileAccess. readWrite); 7 using (stream) 8 {9 SerializableCase serCase = new SerializableCase (); 10 serCase. state = "Test"; 11 formatter. serialize (stream, serCase); 12} 13} 14 15 public static void BinaryFormatterDesSerialize () 16 {17 Stream stream = new FileStream ("jin. glory ", FileMode. open, FileAccess. read); 18 IFormatter formatter = new BinaryFormatter (); 19 using (stream) 20 {21 SerializableCase serCase = formatter. deserialize (stream) as SerializableCase; 22 return serCase. state; 23} 24} 25} copy the code BinaryFormattSerialize () method to instantiate the SerializableCase type and assign values to the State attribute to indicate a State. After MySerializableCase. BinaryFormattSerialize () is called,. NET saves the serialized file stream to the jin. glory file. The name and suffix format of the file in Figure 1 are customized by yourself. Then call deserialization to get the status of the previously serialized object. 1 string state = MySerializableCase. binaryFormattDesSerialize (); 2 Console. writeLine (state); the SOAP formatter SoapFormatter Is In The namespace System. runtime. serialization. formatters. under Soap (in System. runtime. serialization. formatters. soap. dll) Copy code 1 public class MySerializableCase 2 {3 public static void SoapFormatterSerialize () 4 {5 IFormatter formatter = new SoapFormatter (); 6 Stream stream = new FileStream ("Soap. xml", FileMode. create, FileAccess. readWrite); 7 using (stream) 8 {9 SerializableCase serCase = new SerializableCase (); 10 serCase. state = "Test"; 11 formatter. serialize (stream, serCase); 12} 13} 14 15 public static string SoapFormatterDesSerialize () 16 {17 Stream stream = new FileStream ("Soap. xml ", FileMode. open, FileAccess. read); 18 IFormatter formatter = new SoapFormatter (); 19 using (stream) 20 {21 Seria LizableCase serCase = formatter. deserialize (stream) as SerializableCase; 22 return serCase. state; 23} 24} 25} the copied code is similar to the method used by the preceding binary formatter. The only difference is that the serialized file generated by the Soap formatter takes longer, it also occupies a large amount of space, but it complies with the SOAP protocol, which is useful for cross-platform data operations. The platform can re-generate objects only by parsing and rebuilding them. The Serializable Member cannot copy code 1 [Serializable] 2 public class SerializableCase 3 {4 public SerializableCase () {} 5 6 private string _ State; 7 8 public string State 9 {10 get {return _ State;} 11 set {_ State = value;} 12} 13 14 [NonSerialized] 15 private DonotSerializable _ DonotSerializable; 16 17 public DonotSerializable DonotSerializable18 {19 get {return _ DonotSerializable;} 20 set {_ DonotSerializable = value ;} 21} 22} 23 public class DonotSerializable24 {25 public DonotSerializable () {} 26 27 public string DonotSerializableData28 {29 get; 30 set; 31} 32} copied the code to modify the sample code in the first section, added an attribute, and set its field to NonSerialized. NET will skip this class when detecting [NonSerialized] During serialization of this class, because some objects are indeed not suitable for serialization for persistence, however, this may also cause a problem, that is, the object state is lost, that is, the unserializable part will be lost. Check out the following code: Copy code 1 public class MySerializableCase 2 {3 public static void BinaryFormatterSerialize () 4 {5 IFormatter formatter = new BinaryFormatter (); 6 Stream stream = new FileStream ("jin. glory ", FileMode. create, FileAccess. readWrite); 7 using (stream) 8 {9 SerializableCase serCase = new SerializableCase (); 10 serCase. state = "Test"; 11 serCase. donotSerializable = new DonotSerializable (); 12 serCa Se. donotSerializable. donotSerializableData = "DonotSerializableData"; 13 formatter. serialize (stream, serCase); 14} 15} 16 17 public static string BinaryFormatterDesSerialize () 18 {19 Stream stream = new FileStream ("jin. glory ", FileMode. open, FileAccess. read); 20 IFormatter formatter = new BinaryFormatter (); 21 using (stream) 22 {23 SerializableCase serCase = formatter. deserialize (stream) as Serializable Case; 24 return serCase. state + "_" + serCase. donotSerializable. donotSerializableData; 25} 26} 27} copied the code and modified the code of the above binary formatter. Call the test code and let's take a look at the result: 1 MySerializableCase. binaryFormatterSerialize (); 2 string state = MySerializableCase. binaryFormatterDesSerialize (); 3 Console. writeLine (state); During deserialization, The DonotSerializable attribute of the object SerializableCase is lost, so an error is reported. In this case ,. NET provides the IDeserializationCallback interface, which has only one function void OnDeserialization (object sender), as long as IDeserializationCallback is implemented, and the OnDeserialization function implements specific initialization of non-serializable objects. Copy code 1 [Serializable] 2 public class SerializableCase: IDeserializationCallback 3 {4 public SerializableCase () {} 5 6 private string _ State; 7 8 public string State 9 {10 get {return _ State;} 11 set {_ State = value;} 12} 13 14 [NonSerialized] 15 private DonotSerializable _ DonotSerializable; 16 17 public DonotSerializable DonotSerializable18 {19 get {return _ DonotSerializable;} 20 set {_ DonotSer Ializable = value;} 21} 22 23 public void OnDeserialization (object sender) 24 {25 _ DonotSerializable = new DonotSerializable (); 26 _ DonotSerializable. donotSerializableData = "DonotSerializableData-> Test"; 27} 28} copy the code according to the preceding call method. The result is as follows: During deserialization, if the instance type implements the IDeserializationCallback interface, the OnDeserialization () method that implements IDeserializationCallback will be executed at the completion of deserialization, in this way, some non-serializable attribute states can be implemented in this method. Serialization event. NET2.0 introduces support for serialization events. When serialization and deserialization are performed ,. NET calls the specified method on your class ,. NET defines four serialization and deserialization events. The serializing event is triggered before serialization occurs. The serialized event is triggered after serialization, The deserializing event is triggered before deserialization, And the deserialized event is triggered after deserialization. Reference the initial code of the previous sample code SerializableCase class: copy the Code [Serializable] public class SerializableCase //: IDeserializationCallback {public SerializableCase () {} private string _ State; public string State {get {return _ State;} set {_ State = value ;}} the sample code after the event is added by copying the code is as follows: copy code 1 [Serializable] 2 public class SerializableCase 3 {4 public SerializableCase () {} 5 private string _ State; 6 public string State 7 {8 get {return _ State;} 9 set {_ State = value;} 10} 11 12 [OnSerializing] 13 private void OnSerializing (StreamingContext context) 14 {15 _ State = "the current status is: Before serialization"; 16 Console. writeLine (State); 17} 18 19 [OnSerialized] 20 private void OnSerialized (StreamingContext context) 21 {22 _ State = "the status is: after serialization"; 23 Console. writeLine (State); 24} 25 26 [OnDeserializing] 27 private void OnDeserializing (StreamingContex T context) 28 {29 _ State = "the current status is: Before deserialization"; 30 Console. writeLine (State); 31} 32 33 [OnDeserialized] 34 private void OnDeserialized (StreamingContext context) 35 {36 _ State = "the status is: after deserialization"; 37 Console. writeLine (State); 38} 39} copy the code to use the static method defined in the MySerializableCase type. For example, copy code 1 public static void SoapFormatterSerialize () 2 {3 IFormatter formatter = new SoapFormatter (); 4 Stream stream = new FileStr Eam ("Soap. xml ", FileMode. create, FileAccess. readWrite); 5 using (stream) 6 {7 SerializableCase serCase = new SerializableCase (); 8 formatter. serialize (stream, serCase); 9} 10} 11 public static string SoapFormatterDesSerialize () 12 {13 Stream stream = new FileStream ("Soap. xml ", FileMode. open, FileAccess. read); 14 IFormatter formatter = new SoapFormatter (); 15 using (stream) 16 {17 SerializableCase ser Case = formatter. deserialize (stream) as SerializableCase; 18 return serCase. state; 19} 20 21} 1 MySerializableCase. soapFormatterSerialize (); 2 MySerializableCase. soapFormatterDesSerialize (); 3 Console. readLine (); it is clearly displayed from the results. Here I want to talk a few more things. Careful friends may find that the signatures of the four event functions in the SerializableCase type are the same, this is because. the delegate defined for this serialization and deserialization event in. NET is the signature, which will be detected during serialization and deserialization of this type of instance. NET will reflect all the functions in the instance to check whether there is a serialization event attached. If yes, it will continue to check the signature of this function. If the function signature also matches The function will be attached to the delegate. You can specify a function to call the function for the event as follows: Copy code 1 [OnSerializing] 2 [OnSerialized] 3 [OnDeserializing] 4 [OnDeserialized] 5 private void OnGenericSerializEventMethod (StreamingContext context) 6 {7 ...... 8} copying the code is complete in this article.