序列化 (Serialization)將對象的狀態(tài)信息轉(zhuǎn)換為可以存儲或傳輸?shù)男问降倪^程。在序列化期間,對象將其當(dāng)前狀態(tài)寫入到臨時(shí)或持久性存儲區(qū)。以后,可以通過從存儲區(qū)中讀取或反序列化對象的狀態(tài),重新創(chuàng)建該對象。(摘自百度百科) 在很多通訊或者數(shù)據(jù)存儲的過程中,都需要序列化和反序列化的過程。 在C#中,如果想要進(jìn)行自定義類的序列化,只需要簡單地在定義類的時(shí)候添加Serializable標(biāo)簽即可。如:
經(jīng)常用到的序列化方式有兩種:二進(jìn)制和XML,分為由System.Runtime.Serialization.Formatters.Binary.BinaryFormatter和System.Xml.Serialization.XmlSerializer實(shí)現(xiàn)。 BinaryFormatter進(jìn)行序列化和反序列化的時(shí)候,不會關(guān)心實(shí)際類型。但是XmlSerializer需要。 自己寫的一個簡單的序列化輔助類 public static class SerializeHelper
{ /// <summary>
/// 使用UTF8編碼將byte數(shù)組轉(zhuǎn)成字符串 /// </summary>
/// <param name='data'></param>
/// <returns></returns>
public static string ConvertToString(byte[] data)
{ return Encoding.UTF8.GetString(data , 0 , data.Length);
} /// <summary>
/// 使用指定字符編碼將byte數(shù)組轉(zhuǎn)成字符串 /// </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編碼將字符串轉(zhuǎn)成byte數(shù)組 /// </summary>
/// <param name='str'></param>
/// <returns></returns>
public static byte[] ConvertToByte(string str)
{ return Encoding.UTF8.GetBytes(str);
} /// <summary>
/// 使用指定字符編碼將字符串轉(zhuǎn)成byte數(shù)組 /// </summary>
/// <param name='str'></param>
/// <param name='encoding'></param>
/// <returns></returns>
public static byte[] ConvertToByte(string str , Encoding encoding)
{ return encoding.GetBytes(str);
} /// <summary>
/// 將對象序列化為二進(jìn)制數(shù)據(jù)
/// </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數(shù)據(jù) /// </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>
/// 將二進(jìn)制數(shù)據(jù)反序列化 /// </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>
/// 將二進(jìn)制數(shù)據(jù)反序列化為指定類型對象 /// </summary>
/// <typeparam name='T'></typeparam>
/// <param name='data'></param>
/// <returns></returns>
public static T DeserializeWithBinary<T>(byte[] data)
{ return (T)DeserializeWithBinary(data);
} /// <summary>
/// 將XML數(shù)據(jù)反序列化為指定類型對象 /// </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;
}
} 注意,在反序列化的時(shí)候,將byte[]寫入流以后,需要將游標(biāo)移到首位,即將Position置零,否則反序列化將報(bào)錯。 摘自https://www.cnblogs.com/hualuo-code/p/5165367.html |
|