zoukankan      html  css  js  c++  java
  • .net 序列化与反序列化

    1.序列化 反序列化

    C#中如果需要:将一个结构很复杂的类的对象存储起来,或者通过网路传输到远程的客户端程序中去,这时就需要用到序列化,反序列化(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[]数据,反序列成对象

                     

  • 相关阅读:
    线程基础1
    组合数学的卡特兰数 TOJ 3551: Game of Connections
    2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)
    Codeforces Round #430 (Div. 2)
    线代之高斯消元
    牛客网Wannafly模拟赛
    TOJ3039: 材质贴图
    AtCoder Grand Contest 019
    TOJ 3974: Region n条直线m个圆最多将圆分为几个区域
    AIM Tech Round 4 (Div. 2)
  • 原文地址:https://www.cnblogs.com/gc2013/p/4026747.html
Copyright © 2011-2022 走看看