/// <summary> /// 序列化成字节 /// </summary> /// <param name="value">object扩展</param> /// <returns>序列化成字节</returns> public static byte[] ToBytes <T>(this T o, bool compress = false) { return(compress ? SerializeBytes.ToBytes <T>(o).DeflateCompress() : SerializeBytes.ToBytes <T>(o)); //BinaryFormatter bf1 = new BinaryFormatter(); //using (MemoryStream ms = new MemoryStream()) { // bf1.Serialize(ms, value); // return ms.ToArray(); //} }
/// <summary> /// 字节反序列化成对像 /// </summary> /// <typeparam name="T">对像/类型</typeparam> /// <param name="data">字节</param> /// <returns>对像</returns> public static T FromBytes <T>(this byte[] data, bool compress = false) { return(SerializeBytes.FromBytes <T>(compress ? data.DeflateDecompress() : data)); //var formatter = new BinaryFormatter(); //using (MemoryStream ms = new MemoryStream(data)) return (T)formatter.Deserialize(ms); }