自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

深度剖析C#序列化和反序列化

開發(fā) 后端
C#序列化和反序列化程序都是基于工廠模式下的,那么C#序列化和反序列化到底有什么不同之處么?那么本文就向你詳細(xì)介紹C#序列化和反序列化程序的區(qū)別及其應(yīng)用。

C#序列化和反序列化,兩者的程序處理方式基本一致,都是基于工廠模式的,所謂C#序列化就是是將對(duì)象轉(zhuǎn)換為容易傳輸?shù)母袷降倪^程,一般情況下轉(zhuǎn)化打流文件,放入內(nèi)存或者IO文件中。例如,可以序列化一個(gè)對(duì)象,然后使用 HTTP 通過 Internet 在客戶端和服務(wù)器之間傳輸該對(duì)象,或者和其它應(yīng)用程序共享使用。相反的,反序列化根據(jù)流重新構(gòu)造對(duì)象。.NET自帶的有兩種序列化對(duì)象的方式,Xml和binary的,XML 序列化不轉(zhuǎn)換方法、索引器、私有字段或只讀屬性(只讀集合除外)。要序列化對(duì)象的所有字段和屬性(公共的和私有的),請(qǐng)使用 BinaryFormatter,而不要使用 XML 序列化。

C#序列化和反序列化的實(shí)例應(yīng)用剖析:

二進(jìn)制的C#序列化的方式:

例如我們有個(gè)對(duì)象:

  1. [Serializable]public class ClassToSerialize{  
  2. public int id=100;  
  3. public string name="Name";  
  4. }  

需要序列化該對(duì)象,必須在給該類加上Serializable的屬性,然后創(chuàng)建一個(gè)序列化寫入的流:FileStream fileStream = new FileStream("temp.dat", FileMode.Create);然后創(chuàng)建二進(jìn)制格式器:BinaryFormatter b=new BinaryFormatter();然后是序列化:b.Serialize(fileStream,c);,然后關(guān)閉保存流。(可以見下面的例子)

讀取一個(gè)已經(jīng)被序列化的對(duì)象的時(shí)候:操作方式一樣,只是

  1. FileStream fileStream = new FileStream(  
  2. "temp.dat", FileMode.Open,   
  3. FileAccess.Read, FileShare.Read);  
  4. ClassToSerialize c =  
  5. (ClassToSerialize)b.Deserialize(fileStream); 

然后就可以讀取了,完整的例子是:

  1. using System;  
  2. using System.IO;  
  3. using System.Runtime.Serialization;  
  4. using System.Runtime.Serialization.Formatters.Binary;  
  5. public class SerialTest{  
  6. public void SerializeNow(){  
  7. ClassToSerialize c=new ClassToSerialize();  
  8. FileStream fileStream = new FileStream(  
  9. "temp.dat", FileMode.Create);  
  10.  
  11. BinaryFormatter b=new BinaryFormatter();  
  12. b.Serialize(fileStream,c);  
  13. fileStream.Close();  
  14. }  
  15. public void DeSerializeNow(){  
  16. ClassToSerialize c=new ClassToSerialize();  
  17. FileStream fileStream = new FileStream(  
  18. "temp.dat", FileMode.Open,  
  19.  FileAccess.Read,  
  20.  FileShare.Read);  
  21. BinaryFormatter b=new BinaryFormatter();  
  22. //SoapFormatter  
  23. c=(ClassToSerialize)b.Deserialize(fileStream);  
  24. Console.WriteLine(c.name);  
  25. fileStream.Close();  
  26. }  
  27. public static void Main(string[] s){  
  28. SerialTest st=new SerialTest();  
  29. st.SerializeNow();  
  30. st.DeSerializeNow();  
  31. }  
  32. }  
  33. [Serializable]  
  34. public class ClassToSerialize{  
  35. public int id=100;  
  36. public string name="Name";  
  37. }  

這就是自帶的序列化和反序列的操作,但是,很多情況下,一個(gè)對(duì)象比較大,而且很多私有的屬性和方法我們不需要,例如在原型模式里面序列化的話,只需要序列Clone方法和一些屬性,私有的方法無需要,還例如在讀取大規(guī)模的IO的時(shí)候,讀取操作完全不需要... 這時(shí)候就需要自己集成重寫序列的ISerializable接口:

實(shí)現(xiàn)該接口需要兩個(gè)注意的,一個(gè)就是構(gòu)造函數(shù),主要是為了反序列,另一個(gè)就是GetObjectData,主要是執(zhí)行序列化,例如我們現(xiàn)在有一個(gè)Employee類需要序列化

  1. [Serializable()]  
  2. //Set this attribute to all the classes that want to serialize  
  3. public class Employee : ISerializable   
  4. //derive your class from ISerializable {  
  5. public int EmpId;  
  6. public string EmpName;  
  7. [NonSerialized()]  
  8. public string NoSerialString="NoSerialString-Test";  
  9.  

需要注意的是我這里的NoSerialString屬性前面有[NonSerialized()],就是說默認(rèn)并不序列化這個(gè)屬性,而是使用默認(rèn)值 。

首先是構(gòu)造函數(shù):

  1. public Employee(SerializationInfo info, StreamingContext ctxt)  
  2. {  
  3. EmpId = (int)info.GetValue(  
  4. "EmployeeId"typeof(int));  
  5. EmpName = (String)info.GetValue(  
  6. "EmployeeName"typeof(string));  
  7. //NoSerialString =   
  8. //(String)info.GetValue("NoSerialString", typeof(string));  

然后是C#序列化方法,就是當(dāng)寫入流的時(shí)候怎么保存的:

  1. public void GetObjectData(SerializationInfo info, StreamingContext ctxt)  
  2. {  
  3. //You can use any custom name for your name-value pair.  
  4. // But make sure you  
  5. // read the values with the same name.  
  6. //For ex:- If you write EmpId as "EmployeeId"  
  7. // then you should read the same with "EmployeeId"  
  8. info.AddValue("EmployeeId", EmpId);  
  9. info.AddValue("EmployeeName", EmpName);  

把上面兩個(gè)方法寫入到Employee類,然后寫個(gè)測(cè)試的程序:

  1. public class ObjSerial{  
  2. public static void Main(String[] args){  
  3. Employee mp = new Employee();  
  4. mp.EmpId = 10;  
  5. mp.EmpName = "Omkumar";  
  6. mp.NoSerialString = "你好啊";  
  7.  
  8.    //C#序列化和反序列化之序列化  
  9. Stream stream = File.Open("EmployeeInfo.osl", FileMode.Create);  
  10. BinaryFormatter bformatter = new BinaryFormatter();  
  11.  
  12. Console.WriteLine("Writing Employee Information");  
  13. bformatter.Serialize(stream, mp);  
  14. stream.Close();  
  15.  
  16.  
  17. mp = null;  
  18.    //C#序列化和反序列化之反序列  
  19. stream = File.Open("EmployeeInfo.osl", FileMode.Open);  
  20. bformatter = new BinaryFormatter();  
  21.  
  22. Console.WriteLine("Reading Employee Information");  
  23. mp = (Employee)bformatter.Deserialize(stream);  
  24. stream.Close();  
  25.  
  26. Console.WriteLine(  
  27. "Employee Id: {0}",mp.EmpId.ToString());  
  28. Console.WriteLine(  
  29. "Employee Name: {0}",mp.EmpName);  
  30. Console.WriteLine(  
  31. "Employee NoSerialString: {0}",mp.NoSerialString);  
  32.  
  33. }  
  34. }  

C#序列化和反序列化程序執(zhí)行的結(jié)果是:

  1. Writing Employee Information  
  2. Reading Employee Information  
  3. Employee Id: 10  
  4. Employee Name: Omkumar  
  5. Employee NoSerialString: NoSerialString-Test 

看到Employee NoSerialString:屬性的值沒有,它保持默認(rèn)值,沒有序列化。

C#序列化和反序列化的理解就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)更重要的是對(duì)你使用C#序列化和反序列化有所幫助。

【編輯推薦】

  1. C# 泛型集合實(shí)例應(yīng)用淺析
  2. 淺析C# Dictionary泛型集合
  3. C# 泛型編程基礎(chǔ)實(shí)例詳解
  4. 學(xué)習(xí)C#泛型集合類型的心得體會(huì)
  5. .net泛型類的學(xué)習(xí)總結(jié)
責(zé)任編輯:仲衡 來源: 博客園
相關(guān)推薦

2009-08-24 17:14:08

C#序列化

2009-08-06 11:16:25

C#序列化和反序列化

2009-08-25 14:43:26

C#序列化和反序列化

2011-06-01 14:50:48

2022-08-06 08:41:18

序列化反序列化Hessian

2011-06-01 15:05:02

序列化反序列化

2011-05-18 15:20:13

XML

2024-01-30 13:32:51

JSON反序列化序列化

2009-06-14 22:01:27

Java對(duì)象序列化反序列化

2019-11-20 10:07:23

web安全PHP序列化反序列化

2024-03-05 12:49:30

序列化反序列化C#

2021-10-20 07:18:50

Java 序列化漏洞

2023-12-13 13:49:52

Python序列化模塊

2018-03-19 10:20:23

Java序列化反序列化

2021-11-18 07:39:41

Json 序列化Vue

2009-09-09 16:10:11

.NET序列化和反序列

2009-09-09 14:45:41

XML序列化和反序列化

2009-09-09 15:47:27

XML序列化和反序列化

2010-03-19 15:54:21

Java Socket

2009-07-29 13:39:02

JSON序列化和反序列ASP.NET AJA
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)