Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand serialization in .net component programming

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

It is believed that many inexperienced people have no idea about how to understand serialization in .net component programming. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Serialization of .net component programming

Automatic serialization

Serializable attribute

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}

Add the Serializable attribute to the above sample type to mark the sample type as serializable.

Format serializer binary formatter 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}

The BinaryFormattSerialize () method simply instantiates the SerializableCase type and then assigns a value to the State property to represent a state. Net saves the serialized file stream to the jin.glory file after calling MySerializableCase.BinaryFormattSerialize ().

Figure 1

The name and suffix format of the file are randomly defined by yourself. It then invokes deserialization to get the previously serialized object state.

1 string state = MySerializableCase.BinaryFormattDesSerialize (); 2 Console.WriteLine (state)

Figure 2

SOAP formatter

SoapFormatter is under the namespace System.Runtime.Serialization.Formatters.Soap (in System.Runtime.Serialization.Formatters.Soap.dll)

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 SerializableCase serCase = formatter.Deserialize (stream) as SerializableCase;22 return serCase.State;23} 24} 25}

The method used by the above binary formatter is almost the same, except that the serialized file generated by the Soap formatter takes longer and takes up more space, but it follows the SOAP protocol, which is very useful in the cross-platform operation data, the platform only needs to parse and rebuild to regenerate the object.

Nonserializable member 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}

Modify the sample code in the first paragraph, add a property, and set its field to NonSerialized, so that when .NET serializes an instance of this class, it will skip [NonSerialized] when it is detected, because some objects are really not suitable for serialization for persistence, but there is also a problem with the loss of the state of the object, that is, the non-serializable part will be lost. Take a look at the following 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 serCase.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 SerializableCase;24 return serCase.State+ "_" + serCase.DonotSerializable.DonotSerializableData;25} 26} 27}

Modify the code of the above binary formatter and call the test code. Let's take a look at the results:

1 MySerializableCase.BinaryFormatterSerialize (); 2 string state = MySerializableCase.BinaryFormatterDesSerialize (); 3 Console.WriteLine (state)

Figure 3

During deserialization, the DonotSerializable property of the object SerializableCase is lost, which is why the error is reported.

For such cases, .NET provides the IDeserializationCallback interface, which has only one function void OnDeserialization (object sender), as long as it implements IDeserializationCallback and implements specific initialization of non-serializable objects in the OnDeserialization function.

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 {_ DonotSerializable = value;} 21} 22 23 public void OnDeserialization (object sender) 24 {25 _ DonotSerializable = new DonotSerializable (); 26 _ DonotSerializable.DonotSerializableData = "DonotSerializableData- > Test"; 27} 28}

Following the above call, take a look at the result:

Figure 4

This is when deserialization, if it is detected that the instance type implements the IDeserializationCallback interface, the OnDeserialization () method that implements IDeserializationCallback will be executed when the deserialization is complete, so that some non-serializable attribute states can be implemented in this method.

Serialize event

.NET2.0

Introduced support for serialization events. When serializing and deserializing, .NET calls the specified method on your class, and .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

The deserialized event is triggered after deserialization.

Refer to the previous sample code, the initial code of the SerializableCase class:

[Serializable] public class SerializableCase / /: IDeserializationCallback {public SerializableCase () {} private string _ State; public string State {get {return _ State;} set {_ State = value;}

The sample code after adding the event looks like this:

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 = "status at this time is: before serialization"; 16 Console.WriteLine (State); 17} 18 19 [OnSerialized] 20 private void OnSerialized (StreamingContext context) 21 {22 _ State = "status at this time is: after serialization" 23 Console.WriteLine (State); 24} 25 26 [OnDeserializing] 27 private void OnDeserializing (StreamingContext context) 28 {29 _ State = "status at this time is: before deserialization"; 30 Console.WriteLine (State) 31} 32 33 [OnDeserialized] 34 private void OnDeserialized (StreamingContext context) 35 {36 _ State = "status at this time is: after deserialization"; 37 Console.WriteLine (State); 38} 39}

Use the static method in the previously defined MySerializableCase type to demonstrate it with a slight modification

1 public static void SoapFormatterSerialize () 2 {3 IFormatter formatter = new SoapFormatter (); 4 Stream stream = new FileStream ("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 serCase = formatter.Deserialize (stream) as SerializableCase;18 return serCase.State 19} 20 21}

Test the code:

1 MySerializableCase.SoapFormatterSerialize (); 2 MySerializableCase.SoapFormatterDesSerialize (); 3 Console.ReadLine ()

Figure 5

It is obvious from the result that I would like to say a few digresses here. Careful friends may have found that the signatures of the four event functions in the SerializableCase type are all the same, because the delegate defined for this serialization and deserialization event in .NET is this signature. When serializing and deserializing the instance of this type, it will be detected that .NET will reflect all the functions inside the instance to detect whether serialization events have been attached, and if so, it will continue to check the signature of the function. If the function signature also matches, the function will be hung on the delegate.

You can specify a function to specify the calling function for the event:

1 [OnSerializing] 2 [OnSerialized] 3 [OnDeserializing] 4 [OnDeserialized] 5 private void OnGenericSerializEventMethod (StreamingContext context) 6 {7. 8} after reading the above, have you mastered how to understand serialization in .net component programming? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report