如何在不破坏向后兼容性的情况下更改 DataContract 属性 的类型?

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

我正在使用 DataContract 序列化,我不想更改数据成员的类型 属性

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

现在我不想将高度 属性 的类型从 'int' 更改为 'double'。 但是,我不想确保如果我打开一个旧文件,文件中的值会被使用并正确分配给 'Height' 即 int 类型的值转换为 double.

我想在不保留任何冗余属性的情况下执行此操作。 有什么办法可以做到这一点?

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".

所以不,你不能这样做,这违反了合同。

您需要以某种方式削减 运行。您可能需要制作管道来处理旧合同。