7,765
社区成员
发帖
与我相关
我的任务
分享
public class BinarySerialize
{
public static byte[] Serialize<T>(T obj)
{
IFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
byte[] b;
formatter.Serialize(ms, obj);
ms.Position = 0;
b = new byte[ms.Length];
ms.Read(b, 0, b.Length);
ms.Close();
return b;
}
/// <summary>
/// 反序列化二进制代码
/// </summary>
/// <param name="byteArray"></param>
/// <returns></returns>
public static T Deserialize<T>(byte[] byteArray)
{
IFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
ms.Write(byteArray, 0, byteArray.Length);
ms.Position = 0;
return (T)formatter.Deserialize(ms);
}
}