I’m using DataContracts to serialize objects. Suppose I have serialized a data structured in this way:
[DataContract]
public class Dog : IExtensibleDataObject
{
[DataMember]
public int age;
[DataMember]
public string name;
ExtensionDataObject IExtensibleDataObject.ExtensionData { get; set; }
}
Now I’m changing my architecture and I would like to read the previously serialized data with this classes:
[DataContract]
[KnownType(typeof(Dog))]
public class Animal : IExtensibleDataObject
{
[DataMember]
public string name;
ExtensionDataObject IExtensibleDataObject.ExtensionData { get; set; }
}
[DataContract]
public class Dog : Animal
{
[DataMember]
public int age;
}
but I obtain name = null. I know that this depends on the order: the file was saved with age for first and then read starting from name, because it is on the base class.
Is there a way to handle this, maybe by changing the order?
I don’t think that’s possible.
The xml in the past would have been
Its now expecting
The property is higher up in anything that’s serialized on the new DataContract.
Changing inheritance heirarchy is a breaking change with the
IExtensibleDataObjectmethod.More at Best Practices: Data Contract Versioning
Edit 1:
You could perhaps try making Name virtual in Animal and overriding it in Dog? Or some crazy method to force the serialized new version to have name under dog. i.e. something like,