关于序列化与反序列化

本文深入探讨了C#中的序列化概念,包括如何为类添加序列化标签、序列化与反序列化的过程、以及序列化在配置程序加载与保存、分布式计算中的应用。同时,介绍了二进制、XML和SOAP三种序列化机制的特点与使用方法。

关于序列化与反序列化


1、给类加标签,标记可以被序列化  在类上面加 [ Serialize ]  ,要序列化的对象的类与父类均需要标记

2、第二部 准备一个流

FileStream File = new FileStream(@"F:\1.dat",FileMode.Creat,FileAccess.Write)

3、准备可以序列化的工具

BinaryFormatter bin = new BinaryFormatter();

4、进行序列化

Using(file)

{

bin.Serialize(File,list);

}

5、序列化完毕

反序列化

List<student> stu;

1、FileStream File = new FileStream(@"F:\2.txt",FileMode.Open,FileAccess.Read);

2、BinaryFormatter btn = new BinaryFormatter();

3、Using(File)

{

S = (List<Student>) bin.Deserialize(File);

}

-> 序列化就是将对象从内存中移到硬盘中(格式化数据)

-> BinaryFormatter

-> [Serializable]标记

-> 序列化

-> 找到序列化的对象

-> 调用序列化的方法

-> 反序列化

-> 找到流,读出来

-> 找到序列化的对象

-> 调用反序列化的方法



namespace _01序列化
{
    [Serializable]
    class Student
    {
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
        public char Gender
        {
            get;
            set;
        }

        public Student(string n, int a, char g)
        {
            Name = n;
            Age = a;
            Gender = g;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {


            #region 序列化
            List<Student> list = new List<Student>();
            list.Add(new Student("张三1", 19, '男'));
            list.Add(new Student("张三2", 19, '女'));
            list.Add(new Student("张三3", 22, '男'));
            list.Add(new Student("张三4", 19, '女'));
            list.Add(new Student("张三5", 36, '男'));


            // 第一件事,给类加“标签”,标记可以被序列化. 要序列化的对象的类与父类均需标记
            // 第二部,准备一个流
            FileStream file = new FileStream(@"G:\stu.dat", FileMode.Create, FileAccess.Write);
            // 第三步,准备可以序列化的工具
            BinaryFormatter bin = new BinaryFormatter();
            // 进行序列化
            using (file)
            {
                bin.Serialize(file, list);
            }
            Console.WriteLine("序列化完毕");
            #endregion

            #region 反序列化
            // 反序列化
            // 1、准备流
            FileStream file1 = new FileStream(@"G:\stu.dat", FileMode.Open, FileAccess.Read);
            // 2、准备工具
            BinaryFormatter bin1 = new BinaryFormatter();
            List<Student> s;
            // 3、开始反序列化
            using (file)
            {
                s = (List<Student>)bin.Deserialize(file);
            }
            #endregion


            Console.ReadKey();
        }
    }
}


以上是小菜练习的代码~大笑



C#中序列化的应用


序列化是将一个对象保存到存储介质上或者将对象进行转换使之能够在网络上传送的行为。能对一个类进行序列化的条件是:该类的任何基类可序列化;该类应用了Serializable特性。

 

序列化的应用:

 

1、配置程序的加载和保存

 

我们可以创建一个类,它包含了应用程序的配置信息。当应用程序加载时,配置对象被反序列化到内存中的配置类;当用户在程序运行的过程中对配置文件进行修改了的时候,可以把内存的配置类序列化到硬盘。这样,方便地实现了配置文件的读写。

 

2、分布式计算

 

序列化的最大优势在于分布式计算。两台机器拥有相同的程序集,则可以利用序列化技术进行通信。A机器通过序列化技术向B机器发送对象的快照,B机器能快速的、正确地重建出该对象来。

 

.netframework中,有三种序列化机制:二进制、XML和简单对象访问协议(SOAP)。它们的优缺点阐述如下:

 

1、二进制序列化的最大有点是,类型数据可以准确的表示出来。因为二进制序列化对象的共有和私有成员,所以在反序列化的时候可以忠诚地重建出该对象的状态。

 

2XML只序列化对象的公共属性和字段。在XML序列化时,私有字段和其他实例对象就丢失了。

 

3XMLSOAP是开发标准,具有很好的移植性。

 

进行序列化和反序列化的代码很简单,三者对应的名空间及对应的格式化类如下:

 

二进制--System.Runtime.Serialization.Formatters.BinaryBinaryFormatter

 

XML--System.Xml.Serialization—XmlSerializer

 

SOAP--System.Runtime.Serialization.Formatters.SOAP—SoapFormatter

 

序列化和反序列化的代码:

 

[Serializable]

 

   class Test

 

   {

 

       private String name;

 

       public String Name

 

       {

 

           get { return name; }

 

           set { name = value; }

 

       }

 

       private int age;

 

       public int Age

 

       {

 

           get { return age; }

 

           set { age = value; }

 

       }

 

     }

 

Test t =new Test();

 

t.Name ="sanclark";

 

t.Age =22;

 

MemoryStream ms = new MemoryStream();

 

IFormatterbf = new BinaryFormatter();

 

bf.Serialize(ms, t);

 

ms.Seek(0,SeekOrigin.Begin);

 

TestnewObject = (Test) bf.Deserialize(ms);//重建Test对象       

 

序列化的内容是可以定制的,可以通过继承ISerializable接口,重载里面的构造函数和GetObjectData方法即可,此处略去。


转载自http://blog.sina.com.cn/s/blog_645752e60100h4w9.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值