当前位置:   article > 正文

C#[Serializable]在C#中的作用-NET 中的对象序列化_c# [serializable]

c# [serializable]

深入探讨C#序列化和反序列化之前我们先要明白什么是序列化,它又称串行化,是.NET运行时环境用来支持用户定义类型的流化的机制。序列化就是把一个对象保存到一个文件或数据库字段中去,反序列化就是在适当的时候把这个文件再转化成原来的对象使用。其目的是以某种存储形成使自定义对象持久化,或者将这种对象从一个地方传输到另一个地方。.NET框架提供了两种串行化的方式:1、是使用BinaryFormatter进行串行化;2、使用SoapFormatter进行串行化;3、使用XmlSerializer进行串行化。第一种方式提供了一个简单的二进制数据流以及某些附加的类型信息,而第二种将数据流格式化为XML存储;第三种其实和第二种差不多也是XML的格式存储,只不过比第二种的XML格式要简化很多(去掉了SOAP特有的额外信息)。可以使用[Serializable]属性将类标志为可序列化的。如果某个类的元素不想被序列化,1、2可以使用[NonSerialized]属性来标志,2、可以使用[XmlIgnore]来标志。

 

为什么要使用序列化?最重要的两个原因是:将对象的状态保存在存储媒体中以便可以在以后重新创建出完全相同的副本;按值将对象从一个应用程序域发送至另一个应用程序域。例如,序列化可用于在 ASP.NET 中保存会话状态,以及将对象复制到 Windows 窗体的剪贴板中。它还可用于按值将对象从一个应用程序域远程传递至另一个应用程序域。本文简要介绍了 Microsoft .NET 中使用的序列化。

C#序列化和反序列化1、使用BinaryFormatter进行串行化

下面是一个可串行化的类:

  1. using System;    
  2. using System.Data;    
  3. using System.Configuration;    
  4. using System.Web;    
  5. using System.Web.Security;    
  6. using System.Web.UI;    
  7. using System.Web.UI.WebControls;    
  8. using System.Web.UI.WebControls.WebParts;    
  9. using System.Web.UI.HtmlControls;    
  10. using System.IO;    
  11. using System.Runtime.Serialization.Formatters.Binary;    
  12. /**//// ﹤summary﹥    
  13. /// ClassToSerialize 的摘要说明    
  14. /// ﹤/summary﹥    
  15. [Serializable]    
  16. public class ClassToSerialize    
  17. {    
  18.     public int id = 100;    
  19.     public string name = "Name";    
  20.     [NonSerialized]    
  21.     public string Sex = "男";    
  22. }  
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.IO;
  11. using System.Runtime.Serialization.Formatters.Binary;
  12. /** ﹤summary﹥
  13. /// ClassToSerialize 的摘要说明
  14. /// ﹤/summary﹥
  15. [Serializable]
  16. public class ClassToSerialize
  17. {
  18. public int id = 100;
  19. public string name = "Name";
  20. [NonSerialized]
  21. public string Sex = "男";
  22. }

 

下面是串行化和反串行化的方法:

 
  1. public void SerializeNow()    
  2. {    
  3.     ClassToSerialize c = new ClassToSerialize();    
  4.     FileStream fileStream =     
  5.     new FileStream("c:\\temp.dat", FileMode.Create);    
  6.     BinaryFormatter b = new BinaryFormatter();    
  7.     b.Serialize(fileStream, c);    
  8.     fileStream.Close();    
  9. }    
  10. public void DeSerializeNow()    
  11. {    
  12.     ClassToSerialize c = new ClassToSerialize();    
  13.     c.Sex = "kkkk";    
  14.     FileStream fileStream =    
  15.     new FileStream("c:\\temp.dat",     
  16.     FileMode.Open, FileAccess.Read, FileShare.Read);    
  17.     BinaryFormatter b = new BinaryFormatter();    
  18.     c = b.Deserialize(fileStream) as ClassToSerialize;    
  19.     Response.Write(c.name);    
  20.     Response.Write(c.Sex);    
  21.     fileStream.Close();    
  22. }   
  1. public void SerializeNow()
  2. {
  3. ClassToSerialize c = new ClassToSerialize();
  4. FileStream fileStream =
  5. new FileStream("c:\\temp.dat", FileMode.Create);
  6. BinaryFormatter b = new BinaryFormatter();
  7. b.Serialize(fileStream, c);
  8. fileStream.Close();
  9. }
  10. public void DeSerializeNow()
  11. {
  12. ClassToSerialize c = new ClassToSerialize();
  13. c.Sex = "kkkk";
  14. FileStream fileStream =
  15. new FileStream("c:\\temp.dat",
  16. FileMode.Open, FileAccess.Read, FileShare.Read);
  17. BinaryFormatter b = new BinaryFormatter();
  18. c = b.Deserialize(fileStream) as ClassToSerialize;
  19. Response.Write(c.name);
  20. Response.Write(c.Sex);
  21. fileStream.Close();
  22. }

 

调用上述两个方法就可以看到串行化的结果:Sex属性因为被标志为[NonSerialized],故其值总是为null。

 

C#序列化和反序列化2、使用SoapFormatter进行串行化

和BinaryFormatter类似,我们只需要做一下简单修改即可:

a.将using语句中的.Formatter.Binary改为.Formatter.Soap;

b.将所有的BinaryFormatter替换为SoapFormatter.

c.确保报存文件的扩展名为.xml

经过上面简单改动,即可实现SoapFormatter的串行化,这时候产生的文件就是一个xml格式的文件。

 

C#序列化和反序列化3、使用XmlSerializer进行串行化

关于格式化器还有一个问题,假设我们需要XML,但是不想要SOAP特有的额外信息,那么我们应该怎么办呢?有两中方案:要么编写一个实现IFormatter接口的类,采用的方式类似于SoapFormatter类,但是没有你不需要的信息;要么使用库类XmlSerializer,这个类不使用Serializable属性,但是它提供了类似的功能。

如果我们不想使用主流的串行化机制,而想使用XmlSeralizer进行串行化我们需要做一下修改:

a.添加System.Xml.Serialization命名空间。

b.Serializable和NoSerialized属性将被忽略,而是使用XmlIgnore属性,它的行为与NoSerialized类似。

c.XmlSeralizer要求类有个默认的构造器,这个条件可能已经满足了。

下面看C#序列化和反序列化示例:

要序列化的类:
  1. using System;    
  2. using System.Data;    
  3. using System.Configuration;    
  4. using System.Web;    
  5. using System.Web.Security;    
  6. using System.Web.UI;    
  7. using System.Web.UI.WebControls;    
  8. using System.Web.UI.WebControls.WebParts;    
  9. using System.Web.UI.HtmlControls;    
  10. using System.Xml.Serialization;   
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Xml.Serialization; 
 
  1.    
  2. [Serializable]    
  3. public class Person    
  4. {    
  5.     private string name;    
  6.     public string Name    
  7.     {    
  8.         get   
  9.         {    
  10. <span style="white-space: pre;">    </span>    return name;    
  11. <span style="white-space: pre;">    </span>}    
  12. <span style="white-space: pre;">    </span>set   
  13. <span style="white-space: pre;">    </span>{    
  14. <span style="white-space: pre;">    </span>    name = value;    
  15. <span style="white-space: pre;">    </span>}    
  16.     }    
  17.    
  18.    
  19.     public string Sex;    
  20.     public int Age = 31;    
  21.     public Course[] Courses;    
  22.    
  23.     public Person()    
  24.     {    
  25.     }    
  26.     public Person(string Name)    
  27.     {    
  28. <span style="white-space: pre;">    </span>name = Name;    
  29. <span style="white-space: pre;">    </span>Sex = "男";    
  30.     }    
  31. }    
  1. [Serializable]
  2. public class Person
  3. {
  4. private string name;
  5. public string Name
  6. {
  7. get
  8. {
  9. return name;
  10. }
  11. set
  12. {
  13. name = value;
  14. }
  15. }
  16. public string Sex;
  17. public int Age = 31;
  18. public Course[] Courses;
  19. public Person()
  20. {
  21. }
  22. public Person(string Name)
  23. {
  24. name = Name;
  25. Sex = "男";
  26. }
  27. }  
 
  1. [Serializable]    
  2. public class Course    
  3. {    
  4.     public string Name;    
  5.     [XmlIgnore]    
  6.     public string Description;    
  7.     public Course()    
  8.     {    
  9.     }    
  10.     public Course(string name, string description)    
  11.     {    
  12. <span style="white-space: pre;">    </span>Name = name;    
  13. <span style="white-space: pre;">    </span>Description = description;    
  14.     }    
  15. }   
  1. [Serializable]
  2. public class Course
  3. {
  4. public string Name;
  5. [XmlIgnore]
  6. public string Description;
  7. public Course()
  8. {
  9. }
  10. public Course(string name, string description)
  11. {
  12. Name = name;
  13. Description = description;
  14. }
  15. }

C#序列化和反序列化方法:

 
  1. public void XMLSerialize()    
  2. {    
  3.     Person c = new Person("cyj");    
  4.     c.Courses = new Course[2];    
  5.     c.Courses[0] = new Course("英语""交流工具");    
  6.     c.Courses[1] = new Course("数学","自然科学");    
  7.     XmlSerializer xs = new XmlSerializer(typeof(Person));    
  8.     Stream stream = new FileStream("c:\\cyj.XML",FileMode.Create,FileAccess.Write,FileShare.Read);    
  9.     xs.Serialize(stream,c);    
  10.     stream.Close();    
  11. }    
  12. public void XMLDeserialize()    
  13. {    
  14.     XmlSerializer xs = new XmlSerializer(typeof(Person));    
  15.     Stream stream = new FileStream("C:\\cyj.XML",FileMode.Open,FileAccess.Read,FileShare.Read);    
  16.     Person p = xs.Deserialize(stream) as Person;    
  17.     Response.Write(p.Name);    
  18.     Response.Write(p.Age.ToString());    
  19.     Response.Write(p.Courses[0].Name);    
  20.     Response.Write(p.Courses[0].Description);    
  21.     Response.Write(p.Courses[1].Name);    
  22.     Response.Write(p.Courses[1].Description);    
  23.     stream.Close();    
  24. }   
  1. public void XMLSerialize()
  2. {
  3. Person c = new Person("cyj");
  4. c.Courses = new Course[2];
  5. c.Courses[0] = new Course("英语", "交流工具");
  6. c.Courses[1] = new Course("数学","自然科学");
  7. XmlSerializer xs = new XmlSerializer(typeof(Person));
  8. Stream stream = new FileStream("c:\\cyj.XML",FileMode.Create,FileAccess.Write,FileShare.Read);
  9. xs.Serialize(stream,c);
  10. stream.Close();
  11. }
  12. public void XMLDeserialize()
  13. {
  14. XmlSerializer xs = new XmlSerializer(typeof(Person));
  15. Stream stream = new FileStream("C:\\cyj.XML",FileMode.Open,FileAccess.Read,FileShare.Read);
  16. Person p = xs.Deserialize(stream) as Person;
  17. Response.Write(p.Name);
  18. Response.Write(p.Age.ToString());
  19. Response.Write(p.Courses[0].Name);
  20. Response.Write(p.Courses[0].Description);
  21. Response.Write(p.Courses[1].Name);
  22. Response.Write(p.Courses[1].Description);
  23. stream.Close();
  24. }

这里Course类的Description属性值将始终为null,生成的xml文档中也没有该节点,如下:

 
  1. ﹤?xml version="1.0"?﹥    
  2. ﹤Person xmlns:xsi=    
  3. "http://www.w3.org/2001/XMLSchema-instance"     
  4. xmlns:xsd="http://www.w3.org/2001/XMLSchema"﹥    
  5.   ﹤Sex﹥男﹤/Sex﹥    
  6.   ﹤Age﹥31﹤/Age﹥    
  7.   ﹤Courses﹥    
  8. ﹤Course﹥    
  9.   ﹤Name﹥英语﹤/Name﹥    
  10.   ﹤Description﹥交流工具﹤/Description﹥    
  11. ﹤/Course﹥    
  12. ﹤Course﹥    
  13.   ﹤Name﹥数学﹤/Name﹥    
  14.   ﹤Description﹥自然科学﹤/Description﹥    
  15. ﹤/Course﹥    
  16.   ﹤/Courses﹥    
  17.   ﹤Name﹥cyj﹤/Name﹥    
  18. ﹤/Person﹥  
  1. ﹤?xml version="1.0"?﹥
  2. ﹤Person xmlns:xsi=
  3. "http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:xsd="http://www.w3.org/2001/XMLSchema"﹥
  5. ﹤Sex﹥男﹤/Sex﹥
  6. ﹤Age﹥31﹤/Age﹥
  7. ﹤Courses﹥
  8. ﹤Course﹥
  9. ﹤Name﹥英语﹤/Name﹥
  10. ﹤Description﹥交流工具﹤/Description﹥
  11. ﹤/Course﹥
  12. ﹤Course﹥
  13. ﹤Name﹥数学﹤/Name﹥
  14. ﹤Description﹥自然科学﹤/Description﹥
  15. ﹤/Course﹥
  16. ﹤/Courses﹥
  17. ﹤Name﹥cyj﹤/Name﹥
  18. ﹤/Person﹥
 

C#序列化和反序列化4、自定义序列化

如果你希望让用户对类进行串行化,但是对数据流的组织方式不完全满意,那么可以通过在自定义类中实现接口来自定义串行化行为。这个接口只有一个方法,GetObjectData. 这个方法用于将对类对象进行串行化所需要的数据填进SerializationInfo对象。你使用的格式化器将构造SerializationInfo对象,然后在串行化时调用GetObjectData. 如果类的父类也实现了ISerializable,那么应该调用GetObjectData的父类实现。如果你实现了ISerializable,那么还必须提供一个具有特定原型的构造器,这个构造器的参数列表必须与GetObjectData相同。这个构造器应该被声明为私有的或受保护的,以防止粗心的开发人员直接使用它。示例如下:

C#序列化和反序列化之实现ISerializable的类:

  1. using System;    
  2. using System.Data;    
  3. using System.Configuration;    
  4. using System.Web;    
  5. using System.Web.Security;    
  6. using System.Web.UI;    
  7. using System.Web.UI.WebControls;    
  8. using System.Web.UI.WebControls.WebParts;    
  9. using System.Web.UI.HtmlControls;    
  10. using System.Runtime.Serialization;    
  11. using System.Runtime.Serialization.Formatters.Binary;    
  12. /**//// ﹤summary﹥    
  13. /// Employee 的摘要说明    
  14. /// ﹤/summary﹥    
  15. [Serializable]    
  16. public class Employee:ISerializable    
  17. {    
  18.     public int EmpId=100;    
  19.     public string EmpName="刘德华";    
  20.     [NonSerialized]    
  21.     public string NoSerialString = "NoSerialString-Test";    
  22.     public Employee()    
  23.     {    
  24.         //    
  25. <span style="white-space: pre;">    </span>// TODO: 在此处添加构造函数逻辑    
  26. <span style="white-space: pre;">    </span>//    
  27.     }    
  28.     private Employee(SerializationInfo info, StreamingContext ctxt)    
  29.     {    
  30. <span style="white-space: pre;">    </span>EmpId = (int)info.GetValue("EmployeeId"typeof(int));    
  31. <span style="white-space: pre;">    </span>EmpName = (String)info.GetValue("EmployeeName",typeof(string));    
  32. <span style="white-space: pre;">    </span>//NoSerialString = (String)info.GetValue("EmployeeString",typeof(string));    
  33.     }    
  34.     public void GetObjectData(SerializationInfo info, StreamingContext ctxt)    
  35.     {    
  36. <span style="white-space: pre;">    </span>info.AddValue("EmployeeId", EmpId);    
  37. <span style="white-space: pre;">    </span>info.AddValue("EmployeeName", EmpName);    
  38. <span style="white-space: pre;">    </span>//info.AddValue("EmployeeString", NoSerialString);    
  39.     }    
  40. }   
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Runtime.Serialization;
  11. using System.Runtime.Serialization.Formatters.Binary;
  12. /** ﹤summary﹥
  13. /// Employee 的摘要说明
  14. /// ﹤/summary﹥
  15. [Serializable]
  16. public class Employee:ISerializable
  17. {
  18. public int EmpId=100;
  19. public string EmpName="刘德华";
  20. [NonSerialized]
  21. public string NoSerialString = "NoSerialString-Test";
  22. public Employee()
  23. {
  24. //
  25. // TODO: 在此处添加构造函数逻辑
  26. //
  27. }
  28. private Employee(SerializationInfo info, StreamingContext ctxt)
  29. {
  30. EmpId = (int)info.GetValue("EmployeeId", typeof(int));
  31. EmpName = (String)info.GetValue("EmployeeName",typeof(string));
  32. //NoSerialString = (String)info.GetValue("EmployeeString",typeof(string));
  33. }
  34. public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
  35. {
  36. info.AddValue("EmployeeId", EmpId);
  37. info.AddValue("EmployeeName", EmpName);
  38. //info.AddValue("EmployeeString", NoSerialString);
  39. }
  40. }

C#序列化和反序列化方法:

 
  1. public void OtherEmployeeClassTest()    
  2. {    
  3.     Employee mp = new Employee();    
  4.     mp.EmpId = 10;    
  5.     mp.EmpName = "邱枫";    
  6.     mp.NoSerialString = "你好呀";    
  7.     Stream steam = File.Open("c:\\temp3.dat", FileMode.Create);    
  8.     BinaryFormatter bf = new BinaryFormatter();    
  9.     Response.Write("Writing Employee Info:");    
  10.     bf.Serialize(steam,mp);    
  11.     steam.Close();    
  12.     mp = null;    
  13.     //C#序列化和反序列化之反序列化    
  14.     Stream steam2 = File.Open("c:\\temp3.dat", FileMode.Open);    
  15.     BinaryFormatter bf2 = new BinaryFormatter();    
  16.     Response.Write("Reading Employee Info:");    
  17.     Employee mp2 = (Employee)bf2.Deserialize(steam2);    
  18.     steam2.Close();    
  19.     Response.Write(mp2.EmpId);    
  20.     Response.Write(mp2.EmpName);    
  21.     Response.Write(mp2.NoSerialString);    
  22. }   
  1. public void OtherEmployeeClassTest()
  2. {
  3. Employee mp = new Employee();
  4. mp.EmpId = 10;
  5. mp.EmpName = "邱枫";
  6. mp.NoSerialString = "你好呀";
  7. Stream steam = File.Open("c:\\temp3.dat", FileMode.Create);
  8. BinaryFormatter bf = new BinaryFormatter();
  9. Response.Write("Writing Employee Info:");
  10. bf.Serialize(steam,mp);
  11. steam.Close();
  12. mp = null;
  13. //C#序列化和反序列化之反序列化
  14. Stream steam2 = File.Open("c:\\temp3.dat", FileMode.Open);
  15. BinaryFormatter bf2 = new BinaryFormatter();
  16. Response.Write("Reading Employee Info:");
  17. Employee mp2 = (Employee)bf2.Deserialize(steam2);
  18. steam2.Close();
  19. Response.Write(mp2.EmpId);
  20. Response.Write(mp2.EmpName);
  21. Response.Write(mp2.NoSerialString);
  22. }

C#序列化和反序列化的深入探讨就是一个体验和尝试的过程,那么希望本文对你了解和学习C#序列化和反序列化有所帮助。

 
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号