ibcadmin 发表于 2015-2-12 09:33:56

C#中任意对象序列化为二进制byte[]数组

c#中 object对象转为二进制byte[] 以及byte[]转回object
/// <summary>
      /// 把对象序列化为字节数组
      /// </summary>
      public static byte[] SerializeObject(object obj)
      {
            if (obj == null)
                return null;
            MemoryStream ms = new MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(ms, obj);
            ms.Position = 0;
            byte[] bytes = new byte;
            ms.Read(bytes, 0, bytes.Length);
            ms.Close();
            return bytes;
      }

      /// <summary>
      /// 把字节数组反序列化成对象
      /// </summary>
      public static object DeserializeObject(byte[] bytes)
      {
            object obj = null;
            if (bytes == null)
                return obj;
            MemoryStream ms = new MemoryStream(bytes);
            ms.Position = 0;
            BinaryFormatter formatter = new BinaryFormatter();
            obj = formatter.Deserialize(ms);
            ms.Close();
            return obj;
      }

页: [1]
查看完整版本: C#中任意对象序列化为二进制byte[]数组