.net 序列化與還原序列化

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   ar   for   sp   

1.序列化 還原序列化

C#中如果需要:將一個結構很複雜的類的Object Storage Service起來,或者通過網路傳輸到遠端用戶端程式中去,這時就需要用到序列化,還原序列化(Serialization & Deserialization)

 

2.BinaryFormattter

.NET中串列有三種,BinaryFormatter, SoapFormatter和XmlSerializer. 

其中BinaryFormattter最簡單,它是直接用二進位方式把對象 (Object)進行串列或反串,他的優點是速度快,可以串列private或者protected的member, 在不同版本的。NET中都相容,可以看作是。NET自己的本命方法,當然缺點也就隨之而來了,離開了。NET它就活不了,所以不能在其他平台或跨網路上進 行。

3.序列化

  BinaryFormatter ser = new BinaryFormatter();

  MemoryStream ms = new MemoryStream();

  ser.Serialize(ms, DS);

  byte[] buffer = ms.ToArray();

  MemoryStream :建立其支援儲存區為記憶體的流

4.還原序列化

//還原序列化:將byte[]型的資料,放到Stream中,BinaryFormatter將流中的資料還原序列化成對象

  MemoryStream ms = new MemoryStream(bytes);

  BinaryFormatter ser = new BinaryFormatter();

  DataSetSurrogate dss = ser.Deserialize(ms) asDataSetSurrogate;

5.小結

進行序列化,還原序列化,利用到的都是BinaryFormate,都得借普通流MemoryStream,不同的是:

序列化時,將對象序列化後放到MemoryStream,而還原序列化時,將MemoryStream中的byte[]資料,反序列成對象

                 

             
  • SerializeUtil.rar (571 Bytes)

.net 序列化與還原序列化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.