例子:
using System;
using System.Text;
using System.IO;
// Add references to Soap and Binary formatters.
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap ;
using System.Runtime.Serialization;
[Serializable]
public class MyItemType : ISerializable
{
public MyItemType()
{
// Empty constructor required to compile.
}
// The value to serialize.
private string myProperty_value;
public string MyProperty
{
get { return myProperty_value; }
set { myProperty_value = value; }
}
// Implement this method to serialize data. The method is called
// on serialization.
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
// Use the AddValue method to specify serialized values.
info.AddValue("props", myProperty_value, typeof(string));
}
// The special constructor is used to deserialize values.我们在方法GetObjectData中处理序列化,然后在一个带参数的构造方法中处理反序列化。虽然在接口中没有地方指出需要这样一个构造器,但这确实是需要的,除非我们序列化后不再打算把它反序列化回来。
public MyItemType(SerializationInfo info, StreamingContext context)
{
// Reset the property value using the GetValue method.
myProperty_value = (string) info.GetValue("props", typeof(string));
}
}
// This is a console application.
public static class Test
{
static void Main()
{
// This is the name of the file holding the data. You can use any file extension you like.
string fileName = "dataStuff.myData";
// Use a BinaryFormatter or SoapFormatter.
IFormatter formatter = new BinaryFormatter();
//IFormatter formatter = new SoapFormatter();
Test.SerializeItem(fileName, formatter); // Serialize an instance of the class.
Test.DeserializeItem(fileName, formatter); // Deserialize the instance.
Console.WriteLine("Done");
Console.ReadLine();
}
public static void SerializeItem(string fileName, IFormatter formatter)
{
// Create an instance of the type and serialize it.
MyItemType t = new MyItemType();
t.MyProperty = "Hello World";
FileStream s = new FileStream(fileName , FileMode.Create);
formatter.Serialize(s, t);
s.Close();
}
public static void DeserializeItem(string fileName, IFormatter formatter)
{
FileStream s = new FileStream(fileName, FileMode.Open);
MyItemType t = (MyItemType)formatter.Deserialize(s);
Console.WriteLine(t.MyProperty);
}
}
以下是格式化器的工作流程:如果格式化器在序列化一个对象的时候,发现对象继承了ISerializable接口,那它就会忽略掉类型所有的序列化特性,转而调用类型的GetObjectData方法来构造一个SerializationInfo对象,方法内部负责向这个对象添加所有需要序列化的字段
表现出ISerializable接口比Serializable特性的优势的例子:
将Person序列化,然后在反序列化中将其变为另一个对象:PersonAnother类型对象。要实现这个功能,需要Person和PersonAnother都实现ISerializable接口,原来其实很简单,就是在Person类的GetObjectData方法中处理序列化,在PersonAnother的受保护构造方法中反序列化。
class Program { static void Main() { Person liming = new Person() { FirstName = "Ming", LastName = "Li" }; BinarySerializer.SerializeToFile(liming, @"c:\", "person.txt"); PersonAnother p = BinarySerializer.DeserializeFromFile<PersonAnother>(@"c:\person.txt"); Console.WriteLine(p.Name); } } [Serializable] class PersonAnother : ISerializable { public string Name { get; set; } protected PersonAnother(SerializationInfo info, StreamingContext context) { Name = info.GetString("Name"); } void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { } } [Serializable] public class Person : ISerializable { public string FirstName; public string LastName; public string ChineseName; public Person() { } protected Person(SerializationInfo info, StreamingContext context) { } void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { info.SetType(typeof(PersonAnother)); info.AddValue("Name", string.Format("{0} {1}", LastName, FirstName)); } }
在Person类型的GetObjectData方法中,有句代码非常重要:
info.SetType(typeof(PersonAnother));
它负责告诉序列化器:我要被反序列化为PersonAnother。而类型PersonAnother则很简单,它甚至都不需要知道谁会被反序列化成它,它不需要做任何特殊处理。
ISerializable接口这个特性很重要,如果运用得当,在版本升级中,它能处理类型因为字段变化而带来的问题。