using System;
using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Runtime.Serialization.Formatters.Binary; //引用命名空间
namespace ClassLibrary1
{ public class Serialize { #region 二进制序列化 public MemoryStream SerializeBinary(object obj) { BinaryFormatter Serialzer = new BinaryFormatter(); MemoryStream memoryStream = new MemoryStream(); Serialzer.Serialize(memoryStream,obj); return memoryStream; }public byte[] ObjectToBinary(object obj)
{ MemoryStream mem = new MemoryStream(); mem = SerializeBinary(obj); return mem.ToArray(); }public object DeSerialize(MemoryStream mem)
{ mem.Position = 0; BinaryFormatter deserialize = new BinaryFormatter(); object newobject = deserialize.Deserialize(mem); return newobject; }public object ObjectToByte(byte[] buf)
{ MemoryStream mem = new MemoryStream(buf); object obj = this.DeSerialize(mem); return obj; } #endregion }}