注:原书上翻译为串行化,MSDN翻译为序列化,我以MSDN为准,写为序列化。

可以使用属性(Attribute)将类的元素标为可序列化的(Serializable)和不可被序列化的(NonSerialized).NET中有两个类实现了IFormatter借口的类中的SerializeDeserialize方法:BinaryFormatterSoapFormatter。这两个类的区别在于数据流的格式不同。

使用BinaryFormatter进行序列化
在下面这个例子中我们建立一个自定义类型(Insect)集合,使用BinaryFormatter将它们写到二进制文件,然后再将他们读回。
注:以下程序需要导入一些命名空间:
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

序列化(串行化)- 使用BinaryFormatter进行序列化[Serializable]
序列化(串行化)- 使用BinaryFormatter进行序列化
public class Insect
}

我们使用一个标准属性将整个Insect类声明为可序列化的。但是因为一个字段被声明为不可序列化,所以这个字段不能被持久化。

我们先做一个试验,我们只实例化一个Insect对象,创建一个文件,然后使用BinaryFormatter对象和Serialize方法写出这个Insect对象:

序列化(串行化)- 使用BinaryFormatter进行序列化class SerializeApp
}

如果在Visual Studio打开Insect.bin文件就会看到以下内容:
FBinaryFormatter, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Insect name Meadow Brown
(由于我没有截图软件,所以这只是部分内容)
我们可以注意到并没有id字段,因为它没有被序列化。

现在,我们增加几个Insect对象。

序列化(串行化)- 使用BinaryFormatter进行序列化        ArrayList box = new ArrayList();
序列化(串行化)- 使用BinaryFormatter进行序列化        box.Add(
new Insect("Marsh Fritillary"34));
序列化(串行化)- 使用BinaryFormatter进行序列化        box.Add(
new Insect("Speckled Wood"56));
序列化(串行化)- 使用BinaryFormatter进行序列化        box.Add(
new Insect("Milkweed"78));
序列化(串行化)- 使用BinaryFormatter进行序列化        sw 
= File.Open("Insects.bin", FileMode.Append);
序列化(串行化)- 使用BinaryFormatter进行序列化        bf.Serialize(sw, box);
序列化(串行化)- 使用BinaryFormatter进行序列化        sw.Close();
序列化(串行化)- 使用BinaryFormatter进行序列化        
序列化(串行化)- 使用BinaryFormatter进行序列化        Stream sr 
= File.OpenRead("Insects.bin");
序列化(串行化)- 使用BinaryFormatter进行序列化        Insect j 
= (Insect)bf.Deserialize(sr);
序列化(串行化)- 使用BinaryFormatter进行序列化        Console.WriteLine(j);
序列化(串行化)- 使用BinaryFormatter进行序列化        
序列化(串行化)- 使用BinaryFormatter进行序列化        ArrayList bag 
= (ArrayList)bf.Deserialize(sr);
序列化(串行化)- 使用BinaryFormatter进行序列化        sr.Close();
序列化(串行化)- 使用BinaryFormatter进行序列化        
foreach(Insect k in bag)
        }


下面是这个程序的输出:
Meadow Brown:0
Marsh Fritillary:0
Speckled Wood:0
Milkweed:0

id值是0,其原因是很明显的(它在foreach循环中构造Insect的期间被初始化为0)。
注意,我们非常小心地先读回一个Insect对象 - 在读回集合之前已经被序列化到文件的对象。
另外,在我们使用Deserialize时,必须对返回的对象进行类型转换,因为这个方法返回一个一般性的对象。

在后面添加的集合中有三个Insect的数据,这节省了一些开销,因为只需要为第一列的Insect记录Insect类的类型信息。
另外一个有意思的地方是,序列化机制显然能够读写列中的私有字段。

相关文章:

  • 2022-12-23
  • 2021-08-20
  • 2021-06-27
  • 2022-12-23
  • 2022-12-23
  • 2022-02-16
  • 2021-10-22
  • 2022-12-23
猜你喜欢
  • 2021-08-21
  • 2022-12-23
  • 2021-11-19
  • 2021-08-22
  • 2022-12-23
  • 2021-06-24
相关资源
相似解决方案