日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
C#序列化和反序列化

.net下有一種技術(shù)叫做對(duì)象序列化,說(shuō)得通俗一點(diǎn),C#序列化就是把一個(gè)對(duì)象保存到一個(gè)文件或數(shù)據(jù)庫(kù)字段中去,C#反序列化就是在需要的時(shí)候再把這個(gè)文件轉(zhuǎn)化成原來(lái)的對(duì)象使用。

在.NET中常見的C#序列化的方法主要也有三個(gè):二進(jìn)制序列化、XML序列化、SOAP序列化。

下面通過(guò)一個(gè)小例子來(lái)說(shuō)明這三種方法的使用。

 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;   
  4. namespace FileSerializer  
  5. {  
  6.     [Serializable]  
  7.     public class Book  
  8.     {        
  9.         string id;  
  10.         string name;  
  11.  
  12.         public string Id  
  13.         {  
  14.             get { return id; }  
  15.             set { id = value; }  
  16.         }  
  17.  
  18.         public string Name  
  19.         {  
  20.             get { return name; }  
  21.             set { name = value; }  
  22.         }  
  23.           
  24.         public Book()  
  25.         {  
  26.         }  
  27.  
  28.         public Book(string id,string name)  
  29.         {  
  30.             this.id = id;  
  31.             this.name = name;  
  32.         }  
  33.  
  34.         public override string ToString()  
  35.         {  
  36.             return "編號(hào):" + id + "\t名稱:" + name;  
  37.         }  
  38.     }  
  39. }  
  40.  
  41. using System;  
  42. using System.Collections.Generic;  
  43. using System.Text;  
  44. using System.IO;  
  45. using System.Xml.Serialization;  
  46.  
  47. namespace FileSerializer  
  48. {  
  49.     public abstract class Serializer< T>  
  50.     {  
  51.         string filePath;  
  52.  
  53.         public string FilePath  
  54.         {  
  55.             get { return filePath; }  
  56.             set { filePath = value; }  
  57.         }  
  58.  
  59.         public Serializer(string filePath)  
  60.         {  
  61.             this.filePath = filePath;  
  62.         }  
  63.  
  64.         public void Serialize(T serializeObj)  
  65.         {  
  66.             using (Stream st = new FileStream(filePath, FileMode.Create, FileAccess.Write))  
  67.             {  
  68.                 S(st, serializeObj);  
  69.             }  
  70.         }  
  71.  
  72.         protected abstract void S(Stream st, T serializeObj);  
  73.  
  74.         public T Deserialize()  
  75.         {  
  76.             using (Stream st = new FileStream(filePath, FileMode.Open, FileAccess.Read))  
  77.             {  
  78.                 return D(st);  
  79.             }  
  80.         }  
  81.  
  82.         protected abstract T D(Stream st);  
  83.     }  
  84. }  
  85.  
  86. using System;  
  87. using System.Collections.Generic;  
  88. using System.Text;  
  89. using System.IO;  
  90. using System.Runtime.Serialization.Formatters.Binary;  
  91.  
  92. namespace FileSerializer  
  93. {  
  94.     class SerializerBinary< T> : Serializer< T>  
  95.     {  
  96.         public SerializerBinary(string filePath)  
  97.             : base(filePath)  
  98.         {  
  99.         }  
  100.         protected override void S(Stream st, T serializeObj)  
  101.         {  
  102.             BinaryFormatter bf = new BinaryFormatter();  
  103.             bf.Serialize(st, serializeObj);  
  104.         }  
  105.  
  106.         protected override T D(Stream st)  
  107.         {  
  108.             BinaryFormatter bf = new BinaryFormatter();  
  109.             return (T)bf.Deserialize(st);  
  110.         }  
  111.     }  
  112. }  
  113.  
  114. using System;  
  115. using System.Collections.Generic;  
  116. using System.Text;  
  117. using System.IO;  
  118. using System.Runtime.Serialization.Formatters.Soap;  
  119.  
  120. namespace FileSerializer  
  121. {  
  122.     public class SerializerSoap< T> : Serializer< T>  
  123.     {  
  124.         public SerializerSoap(string filePath)  
  125.             : base(filePath)  
  126.         {  
  127.         }  
  128.  
  129.         protected override void S(Stream st, T serializeObj)  
  130.         {  
  131.             SoapFormatter sf = new SoapFormatter();  
  132.             sf.Serialize(st, serializeObj);  
  133.         }  
  134.  
  135.         protected override T D(Stream st)  
  136.         {  
  137.             SoapFormatter sf = new SoapFormatter();  
  138.             return (T)sf.Deserialize(st);  
  139.         }  
  140.     }  
  141. }  
  142.  
  143.  
  144. using System;  
  145. using System.Collections.Generic;  
  146. using System.Text;  
  147. using System.Xml.Serialization;  
  148. using System.IO;  
  149.  
  150. namespace FileSerializer  
  151. {  
  152.     public class SerializerXml< T> : Serializer< T>  
  153.     {  
  154.         public SerializerXml(string filePath)  
  155.             : base(filePath)  
  156.         {  
  157.         }  
  158.  
  159.         protected override void S(Stream st, T serializeObj)  
  160.         {  
  161.             XmlSerializer xs = new XmlSerializer(typeof(T));  
  162.             xs.Serialize(st, serializeObj);  
  163.         }  
  164.  
  165.         protected override T D(Stream st)  
  166.         {  
  167.             XmlSerializer xs = new XmlSerializer(typeof(T));  
  168.             return (T)xs.Deserialize(st);  
  169.         }  
  170.     }  
  171. }  
  172.  
  173.  
  174. using System;  
  175. using System.Collections.Generic;  
  176. using System.Text;  
  177.  
  178. namespace FileSerializer  
  179. {  
  180.     class Program  
  181.     {  
  182.         static void Main(string[] args)  
  183.         {  
  184.             Book book = new Book("01","C#程序設(shè)計(jì)入門01");  
  185.             Serializer< Book> serializer = new SerializerBinary< Book>("bookBinary");  
  186.             serializer.Serialize(book);  
  187.  
  188.             Book newbook = serializer.Deserialize();  
  189.             Console.WriteLine(newbook.ToString());  
  190.      
  191.             book = new Book("02", "C#程序設(shè)計(jì)入門02");  
  192.             serializer = new SerializerSoap< Book>("bookSoap.soap");  
  193.             serializer.Serialize(book);  
  194.  
  195.             newbook = serializer.Deserialize();  
  196.             Console.WriteLine(newbook.ToString());  
  197.  
  198.             book = new Book("03", "C#程序設(shè)計(jì)入門03");  
  199.             serializer = new SerializerXml< Book>("bookXml.xml");  
  200.             serializer.Serialize(book);  
  201.  
  202.             newbook = serializer.Deserialize();  
  203.             Console.WriteLine(newbook.ToString());  
  204.             Console.ReadLine();  
  205.         }  
  206.     }  

C#序列化和反序列化的例子就和大家討論到這里。

【編輯推薦】

  1. C#控制臺(tái)應(yīng)用程序的基本結(jié)構(gòu)
  2. C#編程:使用迭代器
  3. 淺談C#泛型的定義、繼承、方法和約束
  4. C++和C#相互調(diào)用COM組件的方法簡(jiǎn)介
  5. 如何實(shí)現(xiàn)C#代理(Delegate)

文章標(biāo)題:C#序列化和反序列化
當(dāng)前鏈接:http://www.dlmjj.cn/article/ccsjdgd.html