• 序列化和反序列化


    serialize(序列化)和deserialize(反序列化)

    常用的两种序列化方法二进制和xml

    BinaryFormatter

    代码示例

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace TestSerializable
     8 {
     9     [Serializable]
    10     public class Person
    11     {
    12         public string name;
    13 
    14         public int age;
    15 
    16         [NonSerialized]
    17         public string sex;
    18 
    19         public void SayHi()
    20         {
    21             Console.WriteLine("我叫{0},今年{1}岁,我是{2}",this.name,this.age,this.sex);
    22         }
    23         
    24     }
    25 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.IO;
     4 using System.Linq;
     5 using System.Runtime.Serialization.Formatters.Binary;
     6 using System.Text;
     7 using System.Threading.Tasks;
     8 
     9 namespace TestSerializable
    10 {
    11     class Program
    12     {
    13         static void Main(string[] args)
    14         {
    15             //序列化
    16             Person person = new Person();
    17             person.age = 18;
    18             person.name = "Jack";
    19             person.sex = "man";
    20 
    21             FileStream stream = new FileStream(@"c:	empPerson.txt", FileMode.Create);
    22             BinaryFormatter bform = new BinaryFormatter();
    23             bform.Serialize(stream, person);
    24             stream.Close();
    25 
    26             //反序列化
    27             FileStream destream = new FileStream(@"c:	empPerson.txt", FileMode.Open, FileAccess.Read);
    28             var p = (Person)bform.Deserialize(destream);
    29             destream.Close();
    30             p.SayHi();
    31             Console.ReadKey();            
    32         }
    33     }
    34 }
    View Code

    二进制序列化需引用System.Runtime.Serialization.Formatters.Binary

    类被标注[Serializable],无需序列化的成员可用[NonSerialized]

    序列化步骤:

    1 创建对象及字段赋值

    2 创建读写流

    3 创建二进制对象

    4 调用Serialize方法序列化对象

    5 关闭读写流

     

    反序列化步骤:

    1 创建读写流

    2 创建二进制对象

    3 调用Deserialize反序列化

    4 调用对象的字段等

    5 关闭读写流

     

    输出结果

    注意 调用[NonSerialized]标记的成员,为默认值,比如string类型默认为null

     

    XML

    代码示例

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace TestSerializable
     8 {
     9     public class Person
    10     {
    11         public string name;
    12 
    13         public int age;
    14 
    15         private string sex;
    16 
    17         public Person()
    18         {
    19 
    20         }
    21         public Person(string isex)
    22         {
    23             this.sex = isex;
    24         }
    25        public void SayHi()
    26         {
    27             Console.WriteLine("我叫{0},今年{1}岁,我是{2}",this.name,this.age,this.sex);
    28         }
    29         
    30     }
    31 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.IO;
     4 using System.Linq;
     5 using System.Text;
     6 using System.Threading.Tasks;
     7 using System.Xml.Serialization;
     8 
     9 namespace TestSerializable
    10 {
    11     class Program
    12     {
    13         static void Main(string[] args)
    14         {
    15             //序列化
    16             Person person = new Person("man");
    17             person.age = 18;
    18             person.name = "Jack";
    19 
    20             FileStream stream = new FileStream(@"c:	empPerson.xml", FileMode.Create);
    21             XmlSerializer xs = new XmlSerializer(typeof(Person));
    22             xs.Serialize(stream, person);
    23             stream.Close();
    24 
    25             //反序列化
    26             FileStream destream = new FileStream(@"c:	empPerson.xml", FileMode.Open, FileAccess.Read);
    27             var p = (Person)xs.Deserialize(destream);
    28             destream.Close();
    29             p.SayHi();
    30             Console.ReadKey();            
    31         }
    32     }
    33 }
    View Code

    xml序列化需引用System.Xml.Serialization 无需标记,非public的成员无法序列化

     

    输出结果

    总结:

    1 序列化和反序列化适用于保存对象的当前状态

    2 序列化可用于分布式系统中传输数据

  • 相关阅读:
    delphi自动生成资源的REST CRUD工具
    delphi将*.proto转换为pascal工具
    delphi数据表自动生成rest CRUD和rest api在线文档
    delphi基于泛型结构的数据序列
    unidac连接字符串
    dremio 当前支持的权限
    使用jxray分析jvm heap 信息
    apache spark conenct 提升spark 能力
    dremio udf 参考调用处理
    cube.js 0.30.30 之后自定义driver 开发的一些问题说明
  • 原文地址:https://www.cnblogs.com/arvinzd/p/14254105.html
Copyright © 2020-2023  润新知