如何使用继承或子类对象对 DataContract 序列化程序中的数据成员/属性进行排序?
我遇到的问题是 DataContract Serializer 未按所需顺序呈现数据成员或属性。我有一个数据协定装饰类,它继承自另一个数据协定装饰类,例如它是父级的子级,并且我指定特定属性的排序顺序。
我不确定是否需要使用特殊的配置设置或如何完成此操作,即使它需要自定义排序例程。
基本上,当输出序列化时,我希望“ParentResource”中的某些属性出现在“ChildResource”中的某些属性之前。
系列化表示中的理想属性排序: - 父属性1 - 父属性2 - 子属性1 - ChildProperty2
当前属性排序以序列化表示: - 子属性1 - 儿童财产2 - 父属性1 - 父属性2
using System.Runtime.Serialization;
namespace DataContractSerializationOrderingError
{
[DataContract]
[KnownType(typeof(ParentResource))]
public class ChildResource : ParentResource
{
[DataMember(EmitDefaultValue = false)]
public int ChildProperty1 { get; set; }
[DataMember(EmitDefaultValue = false)]
public int ChildProperty2 { get; set; }
}
[DataContract]
public class ParentResource
{
[DataMember(EmitDefaultValue = false, Order = 0)]
public int ParentProperty1 { get; set; }
[DataMember(EmitDefaultValue = false, Order = 1)]
public int ParentProperty2 { get; set; }
}
}
I am having an issue where the DataContract Serializer is not rendering data members or properties in the desired order. I have a data contract adorned class that inherits from another data contract adorned class, e.g. it is the child of a parent, and I specify the sort order for particular properties.
I am not sure if there is a special configuration setting I need to utilize or how to accomplish this, even if it requires a custom sorting routine.
Basically I want a certain properties from the "ParentResource" to appear before some of the properties from the "ChildResource" when the output is serialized.
Ideal Property Ordering In Serialized Representation:
- ParentProperty1
- ParentProperty2
- ChildProperty1
- ChildProperty2
Current Property Ordering In Serialized Representation:
- ChildProperty1
- ChildProperty2
- ParentProperty1
- ParentProperty2
using System.Runtime.Serialization;
namespace DataContractSerializationOrderingError
{
[DataContract]
[KnownType(typeof(ParentResource))]
public class ChildResource : ParentResource
{
[DataMember(EmitDefaultValue = false)]
public int ChildProperty1 { get; set; }
[DataMember(EmitDefaultValue = false)]
public int ChildProperty2 { get; set; }
}
[DataContract]
public class ParentResource
{
[DataMember(EmitDefaultValue = false, Order = 0)]
public int ParentProperty1 { get; set; }
[DataMember(EmitDefaultValue = false, Order = 1)]
public int ParentProperty2 { get; set; }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要的是
DataMember.Order
。What you want is
DataMember.Order
.