序列化和反序列化

using System;
using System.Data;
using System.Configuration;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

[Serializable]
public class serialize
{
public serialize()
{
}
    /// <summary>
    /// 将一个对象序列化
    /// </summary>
    /// <param name="_object"></param>
    /// <returns></returns>
    public static byte[] Serializable(object _object)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        MemoryStream ms = new MemoryStream();
        formatter.Serialize(ms, _object);
        byte[] data = ms.GetBuffer();
        return data;
    }

    /// <summary>
    /// 将字节数组反序列化
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static object Deserialize(byte[] data)
    {
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        System.IO.MemoryStream stream = new System.IO.MemoryStream(data);
        data = null;
        Object obj = formatter.Deserialize(stream);
        return obj;
    }
}

posted on 2009-09-16 13:43  画一个圆圈  阅读(97)  评论(0编辑  收藏  举报

导航