可以使用屬性(Attribute)將類的元素標為可序列化的(Serializable)和不可被序列化的(NonSerialized)。.NET中有兩個類實現了IFormatter借口的類中的Serialize和Deserialize方法:BinaryFormatter和SoapFormatter。這兩個類的區別在於數據流的格式不同。
使用BinaryFormatter進行序列化
在下面這個例子中我們建立一個自定義類型(Insect)集合,使用BinaryFormatter將它們寫到二進制文件,然后再將他們讀回。
注:以下程序需要導入一些命名空間:
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
使用BinaryFormatter進行序列化
在下面這個例子中我們建立一個自定義類型(Insect)集合,使用BinaryFormatter將它們寫到二進制文件,然后再將他們讀回。
注:以下程序需要導入一些命名空間:
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
[Serializable]
public class Insect
{
private string name;
[NonSerialized]
private int id;
public Insect(string name, int id)
{
this.name = name;
this.id= id;
}
public override string ToString()
{
return String.Format("{0}:{1}", name, id);
}
}
public class Insect
{
private string name;
[NonSerialized]
private int id;
public Insect(string name, int id)
{
this.name = name;
this.id= id;
}
public override string ToString()
{
return String.Format("{0}:{1}", name, id);
}
}
我們使用一個標准屬性將整個Insect類聲明為可序列化的。但是因為一個字段被聲明為不可序列化,所以這個字段不能被持久化。
我們先做一個試驗,我們只實例化一個Insect對象,創建一個文件,然后使用BinaryFormatter對象和Serialize方法寫出這個Insect對象:
class SerializeApp
{
public static void Main(string[] args)
{
Insect i = new Insect("Meadow Brown", 12);
Stream sw = File.Create("Insects.bin");
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(sw, i);
sw.Close();
}
}
{
public static void Main(string[] args)
{
Insect i = new Insect("Meadow Brown", 12);
Stream sw = File.Create("Insects.bin");
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(sw, i);
sw.Close();
}
}
如果在Visual Studio打開Insect.bin文件就會看到以下內容:
FBinaryFormatter, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Insect name Meadow Brown(由於我沒有截圖軟件,所以這只是部分內容)
我們可以注意到並沒有id字段,因為它沒有被序列化。
現在,我們增加幾個Insect對象。
ArrayList box = new ArrayList();
box.Add(new Insect("Marsh Fritillary", 34));
box.Add(new Insect("Speckled Wood", 56));
box.Add(new Insect("Milkweed", 78));
sw = File.Open("Insects.bin", FileMode.Append);
bf.Serialize(sw, box);
sw.Close();
Stream sr = File.OpenRead("Insects.bin");
Insect j = (Insect)bf.Deserialize(sr);
Console.WriteLine(j);
ArrayList bag = (ArrayList)bf.Deserialize(sr);
sr.Close();
foreach(Insect k in bag)
{
Console.WriteLine(k);
}
box.Add(new Insect("Marsh Fritillary", 34));
box.Add(new Insect("Speckled Wood", 56));
box.Add(new Insect("Milkweed", 78));
sw = File.Open("Insects.bin", FileMode.Append);
bf.Serialize(sw, box);
sw.Close();
Stream sr = File.OpenRead("Insects.bin");
Insect j = (Insect)bf.Deserialize(sr);
Console.WriteLine(j);
ArrayList bag = (ArrayList)bf.Deserialize(sr);
sr.Close();
foreach(Insect k in bag)
{
Console.WriteLine(k);
}
下面是這個程序的輸出:
Meadow Brown:0
Marsh Fritillary:0
Speckled Wood:0
Milkweed:0
id值是0,其原因是很明顯的(它在foreach循環中構造Insect的期間被初始化為0)。
注意,我們非常小心地先讀回一個Insect對象 - 在讀回集合之前已經被序列化到文件的對象。
另外,在我們使用Deserialize時,必須對返回的對象進行類型轉換,因為這個方法返回一個一般性的對象。
在后面添加的集合中有三個Insect的數據,這節省了一些開銷,因為只需要為第一列的Insect記錄Insect類的類型信息。
另外一個有意思的地方是,序列化機制顯然能夠讀寫列中的私有字段。