using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace TEAMModelOS.SDK.Helper.Common.JsonHelper
{
    /// <summary>
    ///   对象与二进制流的互相转换。
    /// </summary>
    public class ClassSerializers
    {
        #region 对象与二进制流的互相转换
        /// <summary>
        ///   将对象流转换成二进制流
        /// </summary>
        public static MemoryStream SerializeBinary(object request) //将对象流转换成二进制流
        {
            BinaryFormatter serializer = new BinaryFormatter();
            MemoryStream memStream = new MemoryStream(); //创建一个内存流存储区
            serializer.Serialize(memStream, request); //将对象序列化为内存流中
            return memStream;
        }
        /// <summary>
        ///   将二进制流转换成对象
        /// </summary>
        public static object DeSerializeBinary(MemoryStream memStream) //将二进制流转换成对象
        {
            memStream.Position = 0;
            BinaryFormatter deserializer = new BinaryFormatter();
            object newobj = deserializer.Deserialize(memStream); //将内存流反序列化为对象
            memStream.Close(); //关闭内存流，并释放
            return newobj;
        }
        #endregion
    }
}
