I'm supporting a legacy, internal API that uses two classes that are serialized using XML. Both classes are sub-classes of a common base class:
namespace My.Namespace {
[
Serializable,
XmlInclude(typeof(SubClassA)),
XmlInclude(typeof(SubClassB)),
]
public abstract class BaseClass {
// etc.
}
[
Serializable,
XmlType(TypeName="My.Namespace.SubClassA"),
]
public sealed class SubClassA: BaseClass {
// etc.
}
[
Serializable,
XmlType(TypeName="My.Namespace.SubClassB"),
]
public sealed class SubClassB: BaseClass {
// etc.
}
}
So far, so good.
However, the two sub-classes have now been refactored into a single new class (the differences between them are minimal), called SubClass, which also extends BaseClass.
The issue now is that the new class needs to be capable of deserializing legacy XML files that define instances of SubClassA and SubClassB. The problem is that there is no apparent relationship between these types and the new SubClass type. (The SubClassA and SubClassB classes are no longer part of the API and cannot be used to deserialize these instances.)
How do I configure BaseClass and SubClass so that newly serialized XML files define instances of SubClass definitions, while also being able to deserialize legacy XML file instances of SubClassA and SubClassB definitions into instances of SubClass? Can I make SubClassA and SubClassB aliases for SubClass when deserializing XML files?
(Let's assume that the serialized properties are common to SubClass, SubClassA and SubClassB.)