How to serialize the base class with derived classes

You can't magically serialize a derived class as it's base because

"...Serialization checks type of instance by calling Object.getType() method. This method always returns the exact type of object."

http://bytes.com/topic/net/answers/809946-how-force-serialize-base-type

The solution here, if you really need to only serialize the base class is to implement the IXmlSerializable interface and create your own custom serializer.

IXmlSerializable: http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable(v=vs.110).aspx

One more thought. If you can work around the limitation of outputting the extra XML elements, you are able to serialize the derived class using only the base object by either 1) using XmlIncludeAttributes on the base class to tell it which types to expect or 2) using the XmlSerializer constructor overload that takes a list of types.

Edit: After thinking about this a little more, a workaround would be that you would add a Clone() method onto your base object, then serialize the clone of the base.

LinqPad code:

public class Vehicule
{
    public string Name { get; set; }
    public Brand Brand { get; set; }

    public Vehicule Clone()
    {
        return new Vehicule { Name = this.Name, Brand = new Brand { Name = this.Brand.Name } };
    }
}
public class Car : Vehicule
{
    public string Matriculation { get; set; }
}



public class Brand
{
    public string Name { get; set; }
}
public class Renault : Brand
{
    public string Information { get; set; }
}

void Main()
{   
    var car = new Car { Name = "Clio", Matriculation = "XXX-XXX", Brand = new Renault { Name = "Renault", Information = "Contact Infos" } };
    var vehicle = car as Vehicule;

    var serializer = new System.Xml.Serialization.XmlSerializer(typeof(Vehicule));

    XmlWriterSettings settings = new XmlWriterSettings
    {
        Encoding = new UnicodeEncoding(false, false),
        Indent = false,
        OmitXmlDeclaration = false
    };

    using(StringWriter textWriter = new StringWriter())
    using(XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings)) {
        serializer.Serialize(xmlWriter, vehicle.Clone());
        textWriter.ToString().Dump();
    }
}

This is one of the issues with inheritance, and another reason to favor composition imho.

I ran into the same issue on a mobile app where I had a Contact class that derives from ContactSummary. The repository returns Contact instances, but in lots of cases I only wanted the ContactSummary going over the wire to save on message sizes and data usage etc. The default Xml and Json serialisers would only work when the derived class was attributed with the [KnownType()] of the base class, but this still meant all those extra properties going over the wire.

Using inheritance it is problematic to achieve a viable solution, and I didn't want to resort to custom serialisers, and if the solution is to pollute the DTO with copy constructors and clone properties, then why not change the DTO to use composition instead?

If you have control over your DTOs, then restructuring them to use composition rather than inheritance may be the answer. In my example it was fairly simple...

public class ContactSummary
{
    public string Name { get; set;}
    public string Phone { get; set; }
}

public class Contact
{
    public ContactSummary Summary { get; set; }
    // ... other properties here
}

In your example, Car would need to contain a reference to Vehicle not inherit from it - something like...

[KnowTypes(typeof(Renault))]
public class Vehicle
{
    public string Name { get; set; }
    public Brand Brand { get; set; }
}

public class Car
{
    public Vehicle Vehicle { get; set; }
    public string Matriculation { get; set; }
}

Then when you want the 'base' type in your example, simply serialise Car.Vehicle.