Search code examples
c#datacontractserializerdatacontract

How to change the Type of DataContract property without breaking backwards compatibility?


I am working with DataContract Serialization and I wan't to change the type a Datamember property

[DataContract]
public class Page
{
    [DataMember]
    public int Height{get;set;}
}

Now I wan't to change the type of Height property from 'int' to 'double'. However, I wan't to make sure that if I open an old file, the value from the file is used and correctly assigned to 'Height' i.e value of int type casted to double.

I want to do this without having to keep any redundant properties. Is there any way I can achieve this ?


Solution

  • Data Contract Versioning

    The following changes are always breaking

    ...

    • Changing the data contract of a data member. For example, changing the type of data member from an integer to a string, or from a type with a data contract named "Customer" to a type with a data contract named "Person".

    So no, you can't do this, this breaks the contract.

    You will need to cut and run in some way. You may need to make plumbing to deal with older contracts.