創作內容

12 GP

C# 序列化 反序列化

作者:貓貓風 ฅ●ω●ฅ│2019-09-24 14:09:11│巴幣:24│人氣:884
.



















序列化的目的就是能在網路上傳輸物件
否則就無法實現面向物件的分散式計算
Client端要調用Server端的一個方法獲得一個物件

序列化的實現就是把物件變成一個可在網路上傳輸的位元組流。
簡單來說就是把資料轉換成二進制資料可以藉由網路進行傳輸
 
.NET框架提供了兩種種序列化的方式:
1、是使用BinaryFormatter進行序列化;
2、使用XmlSerializer進行序列化。第一種方式提供了一個簡單的二進位制資料流以及某些附加的型別資訊,而第二種將資料流格式化為XML儲存。 可以使用[Serializable]屬性將類標誌為可序列化的。
如果某個類的元素不想被序列化
1、使用[NonSerialized]屬性來標誌
2、使用[XmlIgnore]來標誌


 
※傳輸程式需使用同一個 dll檔建立的class


此篇以資料序列化為二進制檔案的方式來進行實作

Class number

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace Serialization_Test  
  7. {  
  8.     [Serializable] //表示該類可被序列化  
  9.     public class number  
  10.     {  
  11.         public String _str;  
  12.     }  
  13. }  

Class Main


  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Runtime.Serialization.Formatters.Binary; // 序列化 Lib  
  10. using System.IO;  
  11.   
  12. namespace Serialization_Test  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.   
  21.         private number _num = new number();  
  22.         String _Serialize_file_name = "序列化測試.bin";  
  23.         private void btn_Serialize_Click(object sender, EventArgs e)  
  24.         {  
  25.             _num._str = textBox2.Text;  
  26.             //序列化  
  27.             try  
  28.             {  
  29.                 using (FileStream fs = new FileStream(_Serialize_file_name,
  30.                 FileMode.Create))  
  31.                 {  
  32.                     BinaryFormatter bf = new BinaryFormatter();  
  33.                     bf.Serialize(fs, _num);  
  34.                 }  
  35.   
  36.                 txt_process.Text = "序列化成功";  
  37.             }  
  38.             catch(Exception ex)  
  39.             {  
  40.                 txt_process.Text = "序列化失敗 "+ ex.ToString();  
  41.             }  
  42.         }  
  43.   
  44.         private void btn_DeSerialize_Click(object sender, EventArgs e)  
  45.         {  
  46.             textBox1.Clear();  
  47.             //反序列化  
  48.             try  
  49.             {  
  50.                 using (FileStream fs = new FileStream(_Serialize_file_name,
  51.                 FileMode.Open))  
  52.                 {  
  53.                     BinaryFormatter bf = new BinaryFormatter();  
  54.                     number num = bf.Deserialize(fs) as number;  
  55.                     if (num != null)  
  56.                     {  
  57.                         textBox1.Text += num._str;  
  58.                     }  
  59.                 }  
  60.                 txt_process.Text = "反序列化成功";  
  61.             }  
  62.             catch (Exception ex)  
  63.             {  
  64.                 txt_process.Text = "反序列化失敗 " + ex.ToString();  
  65.             }  
  66.         }  
  67.     }  
  68. }  

執行結果

輸入要序列化的資料後按下序列化,畫面會顯示序列化成功


程式將資料序列化為二進制檔案 (.bin)



按下反序列化,將資料從 byte轉換回物件 並呈現原始資料

引用網址:https://home.gamer.com.tw/TrackBack.php?sn=4539227
All rights reserved. 版權所有,保留一切權利

相關創作

同標籤作品搜尋:涼涼風|C#

留言共 0 篇留言

我要留言提醒:您尚未登入,請先登入再留言

12喜歡★s1234567 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:Monster Hunt... 後一篇:C# using Soc...

追蹤私訊切換新版閱覽

作品資料夾

colanncolann
【繪圖創作】【科嵐實驗室】九週年! 2024/4/1 https://home.gamer.com.tw/creationDetail.php?sn=5909407看更多我要大聲說昨天23:46


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】