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

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

相关文章:

  • 2022-12-23
  • 2021-12-19
  • 2021-11-24
  • 2022-01-31
  • 2022-12-23
  • 2022-01-07
  • 2021-12-31
  • 2021-09-20
猜你喜欢
  • 2021-11-06
  • 2021-07-23
  • 2021-08-14
  • 2022-12-23
  • 2021-12-02
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案