序列化 (Serialization)將對象的狀態信息轉換為可以存儲或傳輸的形式的過程。在序列化期間,對象將其當前狀態寫入到臨時或持久性存儲區。以後,可以通過從存儲區中讀取或反序列化對象的狀態,重新創建該對象。(摘自何問起)
在很多通訊或者數據存儲的過程中,都需要序列化和反序列化的過程。
在C#中,如果想要進行自定義類的序列化,只需要簡單地在定義類的時候添加Serializable標簽即可。如:
[Serializable] public class Person { public string name; public int age; }
經常用到的序列化方式有兩種:二進制和XML,分為由System.Runtime.Serialization.Formatters.Binary.BinaryFormatter和System.Xml.Serialization.XmlSerializer實現。
BinaryFormatter進行序列化和反序列化的時候,不會關心實際類型。但是XmlSerializer需要。
自己寫的一個簡單的序列化輔助類
public static class SerializeHelper { /// <summary> /// 使用UTF8編碼將byte數組轉成字符串 /// </summary> /// <param name="data"></param> /// <returns></returns> public static string ConvertToString(byte[] data) { return Encoding.UTF8.GetString(data , 0 , data.Length); } /// <summary> /// 使用指定字符編碼將byte數組轉成字符串 /// </summary> /// <param name="data"></param> /// <param name="encoding"></param> /// <returns></returns> public static string ConvertToString(byte[] data , Encoding encoding) { return encoding.GetString(data , 0 , data.Length); } /// <summary> /// 使用UTF8編碼將字符串轉成byte數組 /// </summary> /// <param name="str"></param> /// <returns></returns> public static byte[] ConvertToByte(string str) { return Encoding.UTF8.GetBytes(str); } /// <summary> /// 使用指定字符編碼將字符串轉成byte數組 /// </summary> /// <param name="str"></param> /// <param name="encoding"></param> /// <returns></returns> public static byte[] ConvertToByte(string str , Encoding encoding) { return encoding.GetBytes(str); } /// <summary> /// 將對象序列化為二進制數據 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static byte[] SerializeToBinary(object obj) { MemoryStream stream = new MemoryStream( ); BinaryFormatter bf = new BinaryFormatter( ); bf.Serialize(stream , obj); byte[] data = stream.ToArray( ); stream.Close( ); return data; } /// <summary> /// 將對象序列化為XML數據 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static byte[] SerializeToXml(object obj) { MemoryStream stream = new MemoryStream( ); XmlSerializer xs = new XmlSerializer(obj.GetType( )); xs.Serialize(stream , obj); byte[] data = stream.ToArray( ); stream.Close( ); return data; } /// <summary> /// 將二進制數據反序列化 /// </summary> /// <param name="data"></param> /// <returns></returns> public static object DeserializeWithBinary(byte[] data) { MemoryStream stream = new MemoryStream( ); stream.Write(data , 0 , data.Length); stream.Position = 0; BinaryFormatter bf = new BinaryFormatter( ); object obj = bf.Deserialize(stream); stream.Close( ); return obj; } /// <summary> /// 將二進制數據反序列化為指定類型對象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <returns></returns> public static T DeserializeWithBinary<T>(byte[] data) { return (T)DeserializeWithBinary(data); } /// <summary> /// 將XML數據反序列化為指定類型對象 /// </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 , 0 , data.Length); stream.Position = 0; XmlSerializer xs = new XmlSerializer(typeof(T)); object obj = xs.Deserialize(stream); stream.Close( ); return (T)obj; } }
注意,在反序列化的時候,將byte[]寫入流以後,需要將游標移到首位,即將Position置零,否則反序列化將報錯。