.net序列化和反序列化

  1. .net c# 序列化和反序列
  2.   所謂的序列化就是是將對象轉換爲容易傳輸的格式的過程,一般情況下轉化打流文件,放入內存或者IO文件中。例如,可以序列化一個對象,然後使用 HTTP 通過 Internet 在客戶端和服務器之間傳輸該對象,或者和其它應用程序共享使用。反之,反序列化根據流重新構造對象。
  3.    
  4.   .NET自帶的有兩種序列化對象的方式,Xml和binary的,XML 序列化不轉換方法、索引器、私有字段或只讀屬性(只讀集合除外)。要序列化對象的所有字段和屬性(公共的和私有的),請使用 BinaryFormatter,而不要使用 XML 序列化(參見ms-help://MS.NETFramework.v20.chs/dv_fxserialization/html/8c63200d-db63-4a03-a93d-21641623df62.htm)
  5. XML 和 SOAP 序列化
  6.    兩者的程序處理方式基本一致,都是基於工廠模式的,下面我就只說二進制的序列化的方式:
  7.    例如我們有個對象: 
  8.  [Serializable]
  9. public class ClassToSerialize{
  10.     public int id=100;
  11.     public string name="Name";
  12. }
  13.    
  14. 需要序列化該對象,必須在給該類加上Serializable的屬性,然後創建一個序列化寫入的流: 
  15. FileStream fileStream = new FileStream("temp.dat", FileMode.Create);
  16. 然後創建二進制格式器: 
  17. BinaryFormatter b=new BinaryFormatter();
  18. 然後是序列化: 
  19. b.Serialize(fileStream,c);
  20. ,然後關閉保存流。(可以見下面的例子)
  21.   
  22.    讀取一個已經被序列化的對象的時候:操作方式一樣,只是 
  23. FileStream fileStream = new FileStream("temp.dat", FileMode.Open, FileAccess.Read, FileShare.Read);
  24. ClassToSerialize c =(ClassToSerialize)b.Deserialize(fileStream);
  25. 然後就可以讀取了,完整的例子是: 
  26. using System;
  27. using System.IO;
  28. using System.Runtime.Serialization;
  29. using System.Runtime.Serialization.Formatters.Binary;
  30. public class SerialTest{
  31.     public void SerializeNow(){
  32.         ClassToSerialize c=new ClassToSerialize();
  33.         FileStream fileStream = new FileStream("temp.dat", FileMode.Create);
  34.         BinaryFormatter b=new BinaryFormatter();
  35.         b.Serialize(fileStream,c);
  36.         fileStream.Close();
  37.     }
  38.     public void DeSerializeNow(){
  39.         ClassToSerialize c=new ClassToSerialize();
  40.         FileStream fileStream = new FileStream("temp.dat", FileMode.Open, FileAccess.Read, FileShare.Read);
  41.         BinaryFormatter b=new BinaryFormatter();//SoapFormatter
  42.         c=(ClassToSerialize)b.Deserialize(fileStream);
  43.         Console.WriteLine(c.name);
  44.         fileStream.Close();
  45.     }
  46.     public static void Main(string[] s){
  47.         SerialTest st=new SerialTest();
  48.         st.SerializeNow();
  49.         st.DeSerializeNow();
  50.     }
  51. }
  52. [Serializable]
  53. public class ClassToSerialize{
  54.     public int id=100;
  55.     public string name="Name";
  56. }
  57. 這就是自帶的序列化和反序列的操作,但是,很多情況下,一個對象比較大,而且很多私有的屬性和方法我們不需要,例如在原型模式裏面序列化的話,只需要序列Clone方法和一些屬性,私有的方法無需要,還例如在讀取大規模的IO的時候,讀取操作完全不需要... 這時候就需要自己集成重寫序列的ISerializable接口:
  58.  實現該接口需要兩個注意的,一個就是構造函數,主要是爲了反序列,另一個就是GetObjectData,主要是執行序列化,例如我們現在有一個Employee類需要序列化 
  59.     [Serializable()]    //Set this attribute to all the classes that want to serialize
  60.     public class Employee : ISerializable //derive your class from ISerializable {
  61.         public int EmpId;
  62.         public string EmpName;
  63.         [NonSerialized()]
  64.     public string NoSerialString="NoSerialString-Test";
  65. }
  66. ,需要注意的是我這裏的NoSerialString屬性前面有[NonSerialized()],就是說默認並不序列化這個屬性,而是使用默認值 。
  67.  首先是構造函數: 
  68.         public Employee(SerializationInfo info, StreamingContext ctxt)
  69.         {
  70.             EmpId = (int)info.GetValue("EmployeeId"typeof(int));
  71.             EmpName = (String)info.GetValue("EmployeeName"typeof(string));
  72.             //NoSerialString = (String)info.GetValue("NoSerialString", typeof(string));
  73.         }
  74. 然後是序列化方法,就是當寫入流的時候怎麼保存的:
  75.         public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
  76.         {
  77.             //You can use any custom name for your name-value pair. But make sure you
  78.             // read the values with the same name. For ex:- If you write EmpId as "EmployeeId"
  79.             // then you should read the same with "EmployeeId"
  80.             info.AddValue("EmployeeId", EmpId);
  81.             info.AddValue("EmployeeName", EmpName);
  82.         }
  83. 把上面兩個方法寫入到Employee類,然後寫個測試的程序:
  84. public class ObjSerial{
  85.     public static void Main(String[] args){
  86.         Employee mp = new Employee();
  87.         mp.EmpId = 10;
  88.         mp.EmpName = "Omkumar";
  89.         mp.NoSerialString = "你好啊";
  90.                 
  91.        //序列化
  92.         Stream stream = File.Open("EmployeeInfo.osl", FileMode.Create);
  93.         BinaryFormatter bformatter = new BinaryFormatter();
  94.                 
  95.         Console.WriteLine("Writing Employee Information");
  96.         bformatter.Serialize(stream, mp);
  97.         stream.Close();
  98.         mp = null;
  99.        //反序列
  100.         stream = File.Open("EmployeeInfo.osl", FileMode.Open);
  101.         bformatter = new BinaryFormatter();
  102.             
  103.         Console.WriteLine("Reading Employee Information");
  104.         mp = (Employee)bformatter.Deserialize(stream);
  105.         stream.Close();
  106.                 
  107.         Console.WriteLine("Employee Id: {0}",mp.EmpId.ToString());
  108.         Console.WriteLine("Employee Name: {0}",mp.EmpName);
  109.         Console.WriteLine("Employee NoSerialString: {0}",mp.NoSerialString);
  110.     }
  111. }
  112. 執行的結果是: 
  113. Writing Employee Information
  114. Reading Employee Information
  115. Employee Id: 10
  116. Employee Name: Omkumar
  117. Employee NoSerialString: NoSerialString-Test
  118.  看到Employee NoSerialString:屬性的值沒有,它保持默認值,沒有序列化。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章