[DataContract(Name = "Person",Namespace = "http://www.cohowinery.com/employees")]
class Person : IExtensibleDataObject
{
private ExtensionDataObject extensionDataObject_value;
public ExtensionDataObject ExtensionData
{
get
{
return extensionDataObject_value;
}
set
{
extensionDataObject_value = value;
}
}
[DataMember]
public string Name;
}
[DataContract(Name = "Person",Namespace = "http://www.cohowinery.com/employees")]
class PersonVersion2
{
[DataMember(Order = 2)]
public int ID;
[DataMember]
public string Name;
private ExtensionDataObject extensionDataObject_value;
public ExtensionDataObject ExtensionData
{
get
{
return extensionDataObject_value;
}
set
{
extensionDataObject_value = value;
}
}
}
public sealed class Program
{
private Program()
{
// Private constructor to prevent creation of this class.
}
public static void Main()
{
try
{
WriteVersion2("V2.xml");
WriteToVersion1("v2.xml");
ReadVersion2("v2.xml");
}
catch (SerializationException exc)
{
Console.WriteLine("{0}: {1}", exc.Message, exc.StackTrace);
}
finally
{
Console.ReadLine();
}
}
// Create an instance of the version 2.0 class. It has
// extra data (ID field) that version 1.0 does
// not understand.
static void WriteVersion2(string path)
{
Console.WriteLine("Creating a version 2 object");
PersonVersion2 p2 = new PersonVersion2();
p2.Name = "Elizabeth";
p2.ID = 2006;
Console.WriteLine("Object data includes an ID");
Console.WriteLine("\t Name: {0}", p2.Name);
Console.WriteLine("\t ID: {0} \n", p2.ID);
// Create an instance of the DataContractSerializer.
DataContractSerializer ser =
new DataContractSerializer(typeof(PersonVersion2));
Console.WriteLine("Serializing the v2 object to a file. \n\n");
FileStream fs = new FileStream(path, FileMode.Create);
ser.WriteObject(fs, p2);
fs.Close();
}
// Deserialize version 2 data to a version 1 object.
static void WriteToVersion1(string path)
{
// Create the serializer using the version 1 type.
DataContractSerializer ser =
new DataContractSerializer(typeof(Person));
FileStream fs = new FileStream(path, FileMode.Open);
XmlDictionaryReader reader =
XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
Console.WriteLine
("Deserializing v2 data to v1 object. \n\n");
Person p1 = (Person)ser.ReadObject(reader, false);
Console.WriteLine("V1 data has only a Name field.");
Console.WriteLine("But the v2 data is stored in the ");
Console.WriteLine("ExtensionData property. \n\n");
Console.WriteLine("\t Name: {0} \n", p1.Name);
fs.Close();
// Change data in the object.
p1.Name = "John";
Console.WriteLine("Changed the Name value to 'John' ");
Console.Write("and reserializing the object to version 2 \n\n");
// Reserialize the object.
fs = new FileStream(path, FileMode.Create);
ser.WriteObject(fs, p1);
fs.Close();
}
// Deserialize a version 2.0 object.
public static void ReadVersion2(string path)
{
FileStream fs = new FileStream(path, FileMode.Open);
DataContractSerializer ser = new DataContractSerializer(typeof(PersonVersion2));
Console.WriteLine("Deserializing new data to version 2 \n\n");
PersonVersion2 p2 = (PersonVersion2)ser.ReadObject(fs);
fs.Close();
Console.WriteLine("The data includes the old ID field value. \n");
Console.WriteLine("\t (New) Name: {0}", p2.Name);
Console.WriteLine("\t ID: {0} \n", p2.ID);
}
}
主要疑问是在代码里
class PersonVersion2的部分,在原例子中,PersonVersion2也要实现接口IExtensibleDataObject,但是如果不实现这个接口的话,整个程序运行也没问题,而且根据整个程序来看的话,好像也没有实现的必要呢,有点不太理解。先标记一下,哪天理解了再说吧!