/// <summary>
/// BinaryHelper 的摘要说明
/// </summary>
public static class BinaryHelper
{
/// <summary>
/// 将对象序列化为Byte[]数组类型,数据库里面对应的字段为:varbinary类型
/// </summary>
/// <param name="obj">obj</param>
/// <returns></returns>
public static byte[] ToByteArr(Object obj)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, obj);
byte[] byteArr = ms.ToArray();
return byteArr;
}
/// <summary>
/// 将Byte[]数组反序列化为一个对象
/// </summary>
/// <typeparam name="T">T</typeparam>
/// <param name="bytes">bytes</param>
/// <returns></returns>
public static T ToObj<T>(byte[] bytes)
{
MemoryStream ms = new MemoryStream(bytes);
BinaryFormatter bf = new BinaryFormatter();
Object obj = bf.Deserialize(ms);
return (T)obj;
}
}
“七”乐无穷,尽在新浪新版博客,快来体验啊~~~请点击进入~