WCF 数据契约与基类和派生类 - 基类更改的后果是什么?
据我了解,您应该使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个突破性的改变。向基类添加新成员时,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.