I am using a child type with no properties in order to place fluent validation rules on the object when it is being used in a particular way.
so
public class User
{
public string name {get;set;}
}
public class myUser : User{}
However when the object gets to my serialiser I want it to serialise it to the base type. so ‘User’ is the root element not ‘myUser’.
Why not simply cast it back to User before doing the serialization?After doing some digging around in the XmlSerializer, this does it for me:
What is being done here is creating an
XmlSerializerwith the base type (User in this case) you want to cast into, as well as an array of other types that you want to allow. These types have to be derived from the base type, it would seem.Output:
Note the
xsi:type="myUser", not sure if this will cause a problem for your deserialization process.