如:一个学生的对象类

 

 

    [Serializable]
    public class Student
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        /// <summary>
        /// 序列化成字节流
        /// </summary>
        /// <returns></returns>
        public byte[] Serialize()
        {
            using (MemoryStream m = new MemoryStream())
            {
                BinaryFormatter f = new BinaryFormatter();
                f.Serialize(m, this);
                m.Position = 0;
                return m.ToArray();
            }
        }
        /// <summary>
        /// 反序列化,把字节流转成对象
        /// </summary>
        public Student Deserialize(byte[] bytes)
        {
            using (MemoryStream m = new MemoryStream())
            {
                BinaryFormatter f = new BinaryFormatter();
                return (Student)f.Deserialize(m);
            }
        }
    }

 

posted on 2010-03-19 14:31  品香一郎  阅读(301)  评论(0编辑  收藏  举报