Serialization (serialization) transforms the state information of an object into a process that can be stored or transmitted in a form. During serialization, an object writes its current state to a temporary or persistent store. Later, the object can be recreated by reading or deserializing the state of the object from the store. (Excerpt from Baidu Encyclopedia)
In the process of many communication or data storage, the process of serialization and deserialization is required.
In C #, if you want to serialize a custom class, simply add the serializable tag when you define the class. Such as:
123456 |
[Serializable] public class Person { public string name; public int age; } |
There are two common ways to serialize: binary and XML, are divided into System.Runtime.Serialization.Formatters.Binary.BinaryFormatter and System.Xml.Serialization.XmlSerializer implementations.
BinaryFormatter does not care about the actual type when it is serialized and deserialized. But XmlSerializer need.
A simple serialization helper class that you write yourself
PublicStaticClassSerializehelper {///<summary>///Convert a byte array to a string using UTF8 encoding///</summary>///<param name= "Data" ></param>///<returns></returns>PublicStaticString ConvertToString (Byte[] data) {return Encoding.UTF8.GetString (data,0, data. Length); }///<summary>///Converts a byte array into a string using the specified character encoding///</summary>///<param name= "Data" ></param>///<param name= "Encoding" ></param>///<returns></returns>PublicStaticString ConvertToString (Byte[] data, Encoding Encoding) {return encoding. GetString (data,0, data. Length); }///<summary>///To convert a string to a byte array using UTF8 encoding///</summary>///<param name= "str" ></param>///<returns></returns>PublicStaticByte[] Converttobyte (StringSTR) {ReturnEncoding.UTF8.GetBytes (str); }///<summary>///Converts a string to a byte array using the specified character encoding///</summary>///<param name= "str" ></param>///<param name= "Encoding" ></param>///<returns></returns>PublicStaticByte[] Converttobyte (StringSTR, Encoding Encoding) {ReturnEncoding. GetBytes (str); }///<summary>///Serializing an object into binary data///</summary>///<param name= "obj" ></param>///<returns></returns>PublicStaticByte[] Serializetobinary (Objectobj) {MemoryStream stream =NewMemoryStream (); BinaryFormatter BF =NewBinaryFormatter (); Bf. Serialize (stream, obj);byte[] data =Stream. ToArray (); Stream. Close ();ReturnData }///<summary>///Serializing objects to XML data///</summary>///<param name= "obj" ></param>///<returns></returns>PublicStaticByte[] Serializetoxml (Objectobj) {MemoryStream stream =NewMemoryStream (); XmlSerializer xs =NewXmlSerializer (obj. GetType ()); Xs. Serialize (stream, obj);byte[] data =Stream. ToArray (); Stream. Close ();ReturnData }///<summary>///Deserialization of binary data///</summary>///<param name= "Data" ></param>///<returns></returns>PublicStaticObject Deserializewithbinary (Byte[] data) {MemoryStream stream =NewMemoryStream (); Stream. Write (data,0, data. Length); Stream. Position =0; BinaryFormatter BF =NewBinaryFormatter ();Object obj =Bf. Deserialize (stream); Stream. Close ();ReturnObj }///<summary>///Deserializes binary data into a specified type object///</summary>///<typeparam name= "T" ></typeparam>///<param name= "Data" ></param>///<returns></returns>PublicStatic T deserializewithbinary<t> (Byte[] data) {ReturnT Deserializewithbinary (data); }///<summary>///Deserializes XML data into a specified type object///</summary>///<typeparam name= "T" ></typeparam>///<param name= "Data" ></param>///<returns></returns> public static T deserializewithxml<t> (byte [] data) {MemoryStream stream = new MemoryStream (); stream. Write (data, 00; XmlSerializer xs = new XmlSerializer (typeof (T)); object obj = XS. Deserialize (stream); Stream. Close (); return (T) obj;}
Note that when deserializing, byte[] is written to the stream, the cursor needs to be moved to the first place and the position zeroed out, otherwise the deserialization will error.
Serialization and deserialization of C # classes