开发者

WCF data contracts with base class and derived classes - what are the consequences of changes to the base class?

开发者 https://www.devze.com 2023-03-09 15:11 出处:网络
As I understand it you should use the Order property of the DataMember attribute so that you can add things to the data contract without the change in order causing things to break, but how should you

As I understand it you should use the Order property of the DataMember attribute so that you can add things to the data contract without the change in order causing things to break, but how should you approach this when you have base and sub types?

If I have datacontracts such as this:

[DataContract]
[KnownType(typeof(ChildDto))]
public class BaseDto
    {
    [DataMember (Name = "Property", Order = 0)]
    public string Property { get; set; }

    [DataMember (Name = 开发者_如何学编程"Property2", Order = 1)]
    public string Property2 { get; set; }
    }

[DataContract]
public class ChildDto:BaseDto
    {
    [DataMember (Name = "Property3", Order = 2)]
    public string Property3 { get; set; }

    [DataMember (Name = "Property4", Order = 3)]
    public string Property4 { get; set; }
    }

and I want to add a new data member property to BaseDto, what order should I give the property so that things don't break? Or should I not add anything to BaseDto? Can I add things to ChildDto?


This is a breaking change. When adding new members to base classes WCF data contract serialization rules always serialize all members from the base class before any of the subclass' members.

You can read more about those rules in this MSDN page titled Data Member Order.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号