格式化程序是一个知道如何将任何对象写到一个流中的对象。IFormatter
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
IFormatter formatter = new SoapFormatter();
完整代码using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
IFormatter formatter = new SoapFormatter();
[SerializableAttribute]
public class TestClass:IDeserializationCallback,ISerializable
{
[NonSerializedAttribute]
public string aa; //不可序列化的字段
public string mystring;
string s = "Wahoo!";
int n = 6;
ArrayList oldStrings=new ArrayList();
static string currentVersion="2.0";
public TestClass(SerializationInfo info, StreamingContext context)
{
//mystring = info.GetString("MyString");
mystring = (string)info.GetValue("MyString", typeof(string));
string streamVersion=info.GetString("Version");
switch(streamVersion)
{
case "1.0":
s=info.GetString("MyString");
n=s.Length;
break;
case "2.0":
s=info.GetString("MyString");
n=s.Length;
oldStrings=(ArrayList)info.GetValue("OldStrings",typeof(ArrayList));
break;
default:
string message=string.Format("Version{0} is not supported.",streamVersion);
throw new SerializationException(message);
}
}
IDeserializationCallback Members
ISerializable Members
}
public class TestClass:IDeserializationCallback,ISerializable
{
[NonSerializedAttribute]
public string aa; //不可序列化的字段
public string mystring;
string s = "Wahoo!";
int n = 6;
ArrayList oldStrings=new ArrayList();
static string currentVersion="2.0";
public TestClass(SerializationInfo info, StreamingContext context)
{
//mystring = info.GetString("MyString");
mystring = (string)info.GetValue("MyString", typeof(string));
string streamVersion=info.GetString("Version");
switch(streamVersion)
{
case "1.0":
s=info.GetString("MyString");
n=s.Length;
break;
case "2.0":
s=info.GetString("MyString");
n=s.Length;
oldStrings=(ArrayList)info.GetValue("OldStrings",typeof(ArrayList));
break;
default:
string message=string.Format("Version{0} is not supported.",streamVersion);
throw new SerializationException(message);
}
}
IDeserializationCallback Members
ISerializable Members
}