WCF 数据契约与基类和派生类 - 基类更改的后果是什么?

发布于 2024-11-12 05:33:27 字数 784 浏览 1 评论 0原文

据我了解,您应该使用 DataMember 属性的 Order 属性,以便可以将内容添加到数据协定中,而不会更改顺序导致内容中断,但是当您有基本类型和子类型时,应该如何处理这个问题?

如果我有这样的数据契约:

[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; }
    }

并且我想向 BaseDto 添加一个新的数据成员属性,我应该为该属性提供什么顺序,以免出现问题?或者我不应该向 BaseDto 添加任何内容?我可以向 ChildDto 添加内容吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝颜夕 2024-11-19 05:33:27

这是一个突破性的改变。向基类添加新成员时,WCF 数据协定序列化规则始终在任何子类成员之前序列化基类中的所有成员。

您可以在标题为 数据成员订单 的 MSDN 页面中了解有关这些规则的更多信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文