深入浅出之C# 串行化与反串行化

来源:互联网 发布:工程造价数据 编辑:程序博客网 时间:2024/05/29 17:48
 序列化又称串行化,是.NET运行时环境用来支持用户定义类型的流化的机制。其目的是以某种存储形成使自定义对象持久化,或者将这种对象从一个地方传输到另一个地方。
串行化是指存储和获取磁盘文件、内存或其他地方中的对象。在串行化时,所有的实例数据都保存到存储介质上,
在取消串行化时,对象会被还原,且不能与其原实例区别开来。只需给类添加Serializable属性,就可以实现串行化实
的成员。反串行化是串行化的逆过程,数据从存储介质中读取出来,并赋给类的实例变量。串行化能保存现有对象的所有状态,
我想我们以前见过的一些游戏的角色账户中的dat文件应该就是被串行化的结果。我尝试了打开一个dat文件,果真得到了
我需要的信息,一些角色的等级之类的信息果然在里面。
 
  .NET框架提供了两种串行化的方式:
 
  1、是使用BinaryFormatter进行串行化;
 
  2、使用SoapFormatter进行串行化;
 
  3、使用XmlSerializer进行串行化。
 
  第一种方式提供了一个简单的二进制数据流以及某些附加的类型信息,而第二种将数据流格式化为XML存储;第三种其实和第二种差不多也是XML的格式存储,只不过比第二种的XML格式要简化很多(去掉了SOAP特有的额外信息)。
 
  可以使用[Serializable]属性将类标志为可序列化的。如果某个类的元素不想被序列化,12可以使用[NonSerialized]属性来标志,2、可以使用[XmlIgnore]来标志。
 
  1、使用BinaryFormatter进行串行化
 
  下面是一个可串行化的类:
 
1.                           using System
2.                          
3.                           using SystemData
4.                          
5.                           using SystemConfiguration
6.                          
7.                           using SystemWeb
8.                          
9.                           using SystemWebSecurity
10.                       
11.                        using SystemWebUI
12.                       
13.                        using SystemWebUIWebControls
14.                       
15.                        using SystemWebUIWebControlsWebParts
16.                       
17.                        using SystemWebUIHtmlControls
18.                       
19.                        using SystemIO
20.                       
21.                        using SystemRuntimeSerializationFormattersBinary
22.                       
23.                        /**//// 
24.                       
25.                        的摘要说明
26.                       
27.                        [Serializable]
28.                       
29.                        public class ClassToSerialize
30.                       
31.                        {
32.                       
33.                        public int id  100
34.                       
35.                        public string name  "Name"
36.                       
37.                        [NonSerialized]
38.                       
39.                        public string Sex  ""
40.                       
41.                        }
42.                       
  下面是串行化和反串行化的方法:
 
1.                           public void SerializeNow()
2.                          
3.                           {
4.                          
5.                           ClassToSerialize c  new ClassToSerialize();
6.                          
7.                           FileStream fileStream  new FileStream"c\\tempdat" FileModeCreate);
8.                          
9.                           BinaryFormatter b  new BinaryFormatter();
10.                       
11.                        bSerializefileStream c);
12.                       
13.                        fileStreamClose();
14.                       
15.                        }
16.                       
17.                        public void DeSerializeNow()
18.                       
19.                        {
20.                       
21.                        ClassToSerialize c  new ClassToSerialize();
22.                       
23.                        cSex  "kkkk"
24.                       
25.                        FileStream fileStream  new FileStream"c\\tempdat" FileModeOpen FileAccessRead FileShareRead);
26.                       
27.                        BinaryFormatter b  new BinaryFormatter();
28.                       
29.                        c  bDeserializefileStream as ClassToSerialize
30.                       
31.                        ResponseWritecname);
32.                       
33.                        ResponseWritecSex);
34.                       
35.                        fileStreamClose();
36.                       
37.                        }
38.                       
  调用上述两个方法就可以看到串行化的结果:Sex属性因为被标志为[NonSerialized],故其值总是为null
 
  2、使用SoapFormatter进行串行化
 
  和BinaryFormatter类似,我们只需要做一下简单修改即可:
 
  a.将using语句中的.FormatterBinary改为.FormatterSoap
 
  b.将所有的BinaryFormatter替换为SoapFormatter
 
  c.确保报存文件的扩展名为.xml
 
  经过上面简单改动,即可实现SoapFormatter的串行化,这时候产生的文件就是一个xml格式的文件。
 
  3、使用XmlSerializer进行串行化
 
  关于格式化器还有一个问题,假设我们需要XML,但是不想要SOAP特有的额外信息,那么我们应该怎么办呢?有两中方案:要么编写一个实现IFormatter接口的类,采用的方式类似于SoapFormatter类,但是没有你不需要的信息;要么使用库类XmlSerializer,这个类不使用Serializable属性,但是它提供了类似的功能。
 
  如果我们不想使用主流的串行化机制,而想使用XmlSeralizer进行串行化我们需要做一下修改:
 
  a.添加SystemXmlSerialization命名空间。
 
  bSerializableNoSerialized属性将被忽略,而是使用XmlIgnore属性,它的行为与NoSerialized类似。
 
  cXmlSeralizer要求类有个默认的构造器,这个条件可能已经满足了。
 
  下面看示例:
 
  要序列化的类:
 
1.                           using System
2.                          
3.                           using SystemData
4.                          
5.                           using SystemConfiguration
6.                          
7.                           using SystemWeb
8.                          
9.                           using SystemWebSecurity
10.                       
11.                        using SystemWebUI
12.                       
13.                        using SystemWebUIWebControls
14.                       
15.                        using SystemWebUIWebControlsWebParts
16.                       
17.                        using SystemWebUIHtmlControls
18.                       
19.                        using SystemXmlSerialization
20.                       
21.                        [Serializable]
22.                       
23.                        public class Person{
24.                       
25.                        private string name
26.                       
27.                        public string Name{
28.                       
29.                        get{
30.                       
31.                        return name
32.                       
33.                        }
34.                       
35.                        set{
36.                       
37.                        name  value
38.                       
39.                        }}
40.                       
41.                        public string Sex
42.                       
43.                        public int Age  31
44.                       
45.                        public Course[] Courses
46.                       
47.                        public Person()
48.                       
49.                        {}
50.                       
51.                        public Personstring Name{
52.                       
53.                        name  Name
54.                       
55.                        Sex  ""
56.                       
57.                        }
58.                       
59.                        }
60.                       
61.                        [Serializable]
62.                       
63.                        public class Course{
64.                       
65.                        public string Name
66.                       
67.                        [XmlIgnore]
68.                       
69.                        public string Description
70.                       
71.                        public Course(){}
72.                       
73.                        public Coursestring name string description{
74.                       
75.                        Name  name
76.                       
77.                        Description  description
78.                       
79.                        }
80.                       
81.                        }
82.                       
  序列化和反序列化方法:
 
1.                           public void XMLSerialize()
2.                          
3.                           {
4.                          
5.                           Person c  new Person"cyj");
6.                          
7.                           cCourses  new Course[2]
8.                          
9.                           cCourses[0]  new Course"英语" "交流工具");
10.                       
11.                        cCourses[1]  new Course"数学""自然科学");
12.                       
13.                        XmlSerializer xs  new XmlSerializertypeofPerson));
14.                       
15.                        Stream stream  new FileStream"c\\cyjXML"FileModeCreateFileAccessWriteFileShareRead);
16.                       
17.                        xsSerializestreamc);
18.                       
19.                        streamClose();
20.                       
21.                        }
22.                       
23.                        public void XMLDeserialize()
24.                       
25.                        {
26.                       
27.                        XmlSerializer xs  new XmlSerializertypeofPerson));
28.                       
29.                        Stream stream  new FileStream"C\\cyjXML"FileModeOpenFileAccessReadFileShareRead);
30.                       
31.                        Person p  xsDeserializestream as Person
32.                       
33.                        ResponseWritepName);
34.                       
35.                        ResponseWritepAgeToString());
36.                       
37.                        ResponseWritepCourses[0]Name);
38.                       
39.                        ResponseWritepCourses[0]Description);
40.                       
41.                        ResponseWritepCourses[1]Name);
42.                       
43.                        ResponseWritepCourses[1]Description);
44.                       
45.                        streamClose();
46.                       
47.                        }
48.                       
  这里Course类的Description属性值将始终为null,生成的xml文档中也没有该节点,如下:
 
1.                           </Sex>
2.                          
3.                           英语</Name>
4.                          
5.                           交流工具
6.                          
7.                           数学</Name>
8.                          
9.                           自然科学
10.                       
  、自定义序列化
 
  如果你希望让用户对类进行串行化,但是对数据流的组织方式不完全满意,那么可以通过在自定义类中实现接口来自定义串行化行为。这个接口只有一个方法,GetObjectData这个方法用于将对类对象进行串行化所需要的数据填进SerializationInfo对象。你使用的格式化器将构造SerializationInfo对象,然后在串行化时调用GetObjectData如果类的父类也实现了ISerializable,那么应该调用GetObjectData的父类实现。
 
  如果你实现了ISerializable,那么还必须提供一个具有特定原型的构造器,这个构造器的参数列表必须与GetObjectData相同。这个构造器应该被声明为私有的或受保护的,以防止粗心的开发人员直接使用它。
 
  示例如下:
 
  实现ISerializable的类:
 
1.                           using System
2.                          
3.                           using SystemData
4.                          
5.                           using SystemConfiguration
6.                          
7.                           using SystemWeb
8.                          
9.                           using SystemWebSecurity
10.                       
11.                        using SystemWebUI
12.                       
13.                        using SystemWebUIWebControls
14.                       
15.                        using SystemWebUIWebControlsWebParts
16.                       
17.                        using SystemWebUIHtmlControls
18.                       
19.                        using SystemRuntimeSerialization
20.                       
21.                        using SystemRuntimeSerializationFormattersBinary
22.                       
23.                        /**//// 
24.                       
25.                        的摘要说明
26.                       
27.                        [Serializable]
28.                       
29.                        public class EmployeeISerializable
30.                       
31.                        {
32.                       
33.                        public int EmpId100
34.                       
35.                        public string EmpName"刘德华"
36.                       
37.                        [NonSerialized]
38.                       
39.                        public string NoSerialString  "NoSerialStringTest"
40.                       
41.                        public Employee()
42.                       
43.                        {
44.                       
45.                        //
46.                       
47.                        // TODO在此处添加构造函数逻辑
48.                       
49.                        //
50.                       
51.                        }
52.                       
53.                        private EmployeeSerializationInfo info StreamingContext ctxt
54.                       
55.                        {
56.                       
57.                        EmpId intinfoGetValue"EmployeeId" typeofint));
58.                       
59.                        EmpName StringinfoGetValue"EmployeeName"typeofstring));
60.                       
61.                        //NoSerialString StringinfoGetValue"EmployeeString"typeofstring));
62.                       
63.                        }
64.                       
65.                        public void GetObjectDataSerializationInfo info StreamingContext ctxt
66.                       
67.                        {
68.                       
69.                        infoAddValue"EmployeeId" EmpId);
70.                       
71.                        infoAddValue"EmployeeName" EmpName);
72.                       
73.                        //infoAddValue"EmployeeString" NoSerialString);
74.                       
75.                        }
76.                       
77.                        }
78.                       
  序列化和反序列化方法:
 
1.                           public void OtherEmployeeClassTest()
2.                          
3.                           {
4.                          
5.                           Employee mp  new Employee();
6.                          
7.                           mpEmpId  10
8.                          
9.                           mpEmpName  "邱枫"
10.                       
11.                        mpNoSerialString  "你好呀"
12.                       
13.                        Stream steam  FileOpen"c\\temp3dat" FileModeCreate);
14.                       
15.                        BinaryFormatter bf  new BinaryFormatter();
16.                       
17.                        ResponseWrite"Writing Employee Info");
18.                       
19.                        bfSerializesteammp);
20.                       
21.                        steamClose();
22.                       
23.                        mp  null
24.                       
25.                        //反序列化
26.                       
27.                        Stream steam2  FileOpen"c\\temp3dat" FileModeOpen);
28.                       
29.                        BinaryFormatter bf2  new BinaryFormatter();
30.                       
31.                        ResponseWrite"Reading Employee Info");
32.                       
33.                        Employee mp2 Employeebf2Deserializesteam2);
34.                       
35.                        steam2Close();
36.                       
37.                        ResponseWritemp2EmpId);
38.                       
39.                        ResponseWritemp2EmpName);
40.                       
41.                        ResponseWritemp2NoSerialString);
42.                       
43.                        }
44.                       
0 0