序列化与反序列化

序列化:是将对象的状态存储到特定的存储介质中的过程,在序列化的过程 ,会将对象的公有成员,私有成员包括类名,都转换成数据流的形式,存储到存储介质中,

反序列化: 是从将特定存储介质中数据重新构建对象的过程.通过反序列化,可以将文件上的对象信息读取,然后重新构建为对象.

一个类要实现序列化,这个类的特性必须被标识为[Serializable]

序列化保存到存储介质上的文件为二进制文件,因此要对二进制进行操作,要引入命名空间:

using System.Runtime.Seriialization.Formatters.Binary;

对二进制操作,就要用到流,要引入命名空间

using System.IO;

下面是实例

1、建立要被序列化的类,用[Serializable]标识类,Students.cs代码如下

using System;

using System.Collections.Generic;

using System.Text;

 

namespace SerializableTest

{

    //特性,表明此类可以被序列化

    [Serializable]

    public class Students

    {

        private string name;

 

        public string Name

        {

            get { return name; }

            set { name = value; }

        }

        private int age;

 

        public int Age

        {

            get { return age; }

            set { age = value; }

        }

    }

}

2、封装序列化、反序列化的方法,SerializableClass.cs代码如下

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Runtime.Serialization.Formatters.Binary;

 

namespace SerializableTest

{

    /// <summary>

    /// 序列化的类

    /// </summary>

    public class SerializableClass

    {

        /// <summary>

        /// 序列化学生类的信息

        /// </summary>

        /// <param name="student"></param>

        public void Serial(Students student)

        {

            FileStream fs = new FileStream(@"c:/test.bin", FileMode.OpenOrCreate);

            BinaryFormatter bf = new BinaryFormatter();

            bf.Serialize(fs, student);

            fs.Close();

        }

 

        /// <summary>

        /// 反序列化学生类的信息

        /// </summary>

        /// <returns></returns>

 

        public Students DeSerial()

        {

            FileStream fs = new FileStream(@"c:/test.bin", FileMode.OpenOrCreate);

            BinaryFormatter bf = new BinaryFormatter();

            Students stu = bf.Deserialize(fs) as Students;

            fs.Close();

            return stu;

 

        }

 

        /// <summary>

        /// 序列化泛型集合

        /// </summary>

        /// <param name="stuList"></param>

        public void SerialList(List<Students> stuList)

        {

 

            FileStream fs = new FileStream(@"c:/test.txt", FileMode.OpenOrCreate);

            BinaryFormatter bf = new BinaryFormatter();

 

            bf.Serialize(fs, stuList);

 

            fs.Close();

 

        }

 

        /// <summary>

        /// 反序列化泛型集合

        /// </summary>

        /// <returns></returns>

        public List<Students> DeSerialList()

        {

            FileStream fs = new FileStream(@"c:/test.txt", FileMode.OpenOrCreate);

            BinaryFormatter bf = new BinaryFormatter();

            List<Students> stuList = bf.Deserialize(fs) as List<Students>;

            fs.Close();

            return stuList;

        }

    }

}

3、如何调用,Program.cs代码如下:

using System;

using System.Collections.Generic;

using System.Text;

 

 

namespace SerializableTest

{

    class Program

    {

        static void Main(string[] args)

        {

 

            SerializableClass sc = new SerializableClass();

            Students student = new Students();

            student.Name = "王五";

            student.Age = 24;

 

            //调用对类序列化的方法

            sc.Serial(student);

            //调用对类反序列化的方法

            Students stuDe = sc.DeSerial() as Students;

            Console.WriteLine("Student类的序列化的输出");

            Console.WriteLine("姓名:"+stuDe.Name +",年龄:"+ stuDe.Age);

 

            //Student 类 泛型集合

            List<Students> stuList = new List<Students>();

 

            Students stu = new Students();

            stu.Name = "张三";

            stu.Age = 21;

            stuList.Add(stu);

 

            Students stu1 = new Students();

            stu1.Name = "李四";

            stu1.Age = 26;

            stuList.Add(stu1);

 

            //调用对泛型序列化的方法

            sc.SerialList(stuList);

 

            //调用对泛型反序列化的方法

            List<Students> stuList1 = sc.DeSerialList();

            foreach (object obj in stuList1)

            {

                Students stuObj = obj as Students;

                Console.WriteLine("姓名:" + stuObj.Name + ",年龄:" + stuObj.Age);

            }

        }

    }

}

 输出:对Student类的序列化的输出
姓名:王五,年龄:24
姓名:张三,年龄:21
姓名:李四,年龄:26
请按任意键继续. . .

参考:

http://blog.163.com/pengpeng_614/blog/static/17707169200863110521697/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值