在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
序列化 (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置零,否则反序列化将报错。
|
请发表评论