JOJ
踏踏实实做人,认认真真做事!放纵自己就是毁灭自己!

    /// <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;
        }
    }

“七”乐无穷,尽在新浪新版博客,快来体验啊~~~请点击进入~

posted on 2010-03-29 23:49  JoinJ  阅读(1080)  评论(0编辑  收藏  举报