C# 二进制BinaryFormatter进行序列化与反序列化

来源:互联网 发布:java lock 编辑:程序博客网 时间:2024/06/10 20:24

本文转载连接: http://blog.csdn.net/e295166319/article/details/52790131?locationNum=4&fps=1


序列化又称串行化,是.NET运行时环境用来支持用户定义类型的流化的机制。其目的是以某种存储形成使自定义对象持久化,或者将这种对象从一个地方传输到另一个地方。

.NET框架提供了两种种串行化的方式:1、是使用BinaryFormatter进行串行化;2、使用XmlSerializer进行串行化。第一种方式提供了一个简单的二进制数据流以及某些附加的类型信息,而第二种将数据流格式化为XML存储。    可以使用[Serializable]属性将类标志为可序列化的。如果某个类的元素不想被序列化,1、可以使用[NonSerialized]属性来标志,2、可以使用[XmlIgnore]来标志。

序列化意思指的是把对象的当前状态进行持久化,一个对象的状态在面向对象的程序中是由属性表示的,所以序列化类的时候是从属性读取值以某种格式保存下来,而类的成员函数不会被序列化,.net存在几种默认提供的序列化,二进制序列化,xml和json序列化会序列化所有的实例共有属性。

[csharp] view plain copy
  1. using UnityEngine;  
  2. using System;  
  3. using System.Collections;  
  4. using System.IO;  
  5. using System.Collections.Generic;  
  6. using System.Runtime.Serialization.Formatters.Binary;  
  7.   
  8. [Serializable]      // 表示该类可以被序列化  
  9. class Person  
  10. {  
  11.     private string name;  
  12.     [NonSerialized]  // 表示下面这个age字段不进行序列化  
  13.     private int age;  
  14.     public string Name  
  15.     {  
  16.         get { return name;}  
  17.         set { name = value;}  
  18.     }  
  19.     public int Age  
  20.     {  
  21.         get { return age;}  
  22.         set { age = value;}  
  23.     }  
  24.     public Person() { }  
  25.     public Person(string name, int age)  
  26.     {  
  27.         this.name = name;  
  28.         this.age = age;  
  29.     }  
  30.   
  31.     public void SayHi()  
  32.     {  
  33.         Debug.LogFormat ("我是{0}, 今年{1}岁", name, age);  
  34.     }  
  35. }  
  36.   
  37.   
  38. public class BinarySerializer : MonoBehaviour {  
  39.   
  40.     string filePath = Directory.GetCurrentDirectory() + "/binaryFile.txt";  
  41.   
  42.     // Use this for initialization  
  43.     void Start () {  
  44.         List<Person> listPers = new List<Person> ();  
  45.         Person per1 = new Person ("张三", 18);  
  46.         Person per2 = new Person ("李四", 20);  
  47.         listPers.Add (per1);  
  48.         listPers.Add (per2);  
  49.         SerializeMethod (listPers);  // 序列化  
  50.         DeserializeMethod();  // 反序列化  
  51.         Debug.Log("Done ! ");  
  52.     }  
  53.   
  54.     void DeserializeMethod()     // 二进制反序列化  
  55.     {  
  56.         FileStream fs = new FileStream (filePath, FileMode.Open);  
  57.         BinaryFormatter bf = new BinaryFormatter ();  
  58.         List<Person> list = bf.Deserialize (fs) as List<Person>;  
  59.   
  60.         if (list != null)   
  61.         {  
  62.             for (int i = 0; i < list.Count; i++)  
  63.             {  
  64.                 list [i].SayHi ();  
  65.             }  
  66.         }  
  67.         fs.Close ();  
  68.     }  
  69.   
  70.     void SerializeMethod(List<Person> listPers)   // 二进制序列化  
  71.     {  
  72.         FileStream fs = new FileStream (filePath, FileMode.Create);  
  73.         BinaryFormatter bf = new BinaryFormatter ();  
  74.         bf.Serialize (fs, listPers);  
  75.         fs.Close ();  
  76.     }  
  77.       
  78.     // Update is called once per frame  
  79.     void Update () {  
  80.       
  81.     }  
  82. }  


序列化的文本打开后,内容如下所示:


反序列化输出结果:

大家好,我是张三,今年0岁

大家好,我是李四,今年0岁

由此看出,未序列化的字段存储的值为空


关于XmlSerializer进行序列化与反序列化的操作将在下篇文章进行介绍……