Pages

Thursday, November 4, 2010

Serialization when using Base class

XmlInclude is the key, it should put on the base class and make the serialization of Automobile feasible.

[System.Xml.Serialization.XmlInclude(typeof(Car))]
[System.Xml.Serialization.XmlInclude(typeof(Truck))]
public class Automobile
{
    public string Name;
    public int Year;
}

public class Car : Automobile
{
    public int NumerOfDoors;
}

public class Truck : Automobile
{
    public bool LongBed;
}

No comments: