序列化/反序列化对象 - 字段顺序很重要吗?
如果字段的顺序不正确(无论这意味着什么),DataContractSerializer
是否可能错误地反序列化对象?
我尝试序列化/反序列化的类没有在字段/属性上放置顺序属性。然而,我的一个字段总是被反序列化为 null
,即使它具有非空值(它实际上包含一个字符串列表)。
当我移动序列化文件中的两个 XML 元素以匹配另一个 XML 示例中的顺序(反序列化工作没有问题)时,一切都开始工作。
这对我来说毫无意义,但也许有人知道得更好。 ;)
Is it possible that DataContractSerializer
wrongly deserializes an object if the fields are not in the "correct" (whatever that means) order?
The classes that I try to serialize/deserialize do not have order-attributes placed on fields/properties. Yet one of my fields always gets deserialized as null
even though it has a non-null value (it actually contains a list of strings).
When I moved two XML elements in serialized file around to match the order in another XML example (for which deserialization worked without problems) everything started to work.
This makes no sense to me but maybe someone knows better. ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了有资格通过
DataContractSerializer
进行正确的序列化/序列化,以下所有条件都必须为真。SerializedAttribute
或DataContractAttribute
;DataMemberAttribute
;IExtensibleDataObject
;DataMemberAttribute
的顺序
-property。因此,声明的顺序确实很重要。如果成员不按字母顺序排列,则会被跳过。查找StackOverflow 上的这个答案作为示例,也许它适用于你的情况。
To be eligible for correct serialization / serialization by the
DataContractSerializer
, all of the following must be true.SerializableAttribute
orDataContractAttribute
set;DataMemberAttribute
set;IExtensibleDataObject
;Order
-property of theDataMemberAttribute
.So, the order of the declaration does matter. If members are not in alphabetical order, they are skipped. Look up this answer at StackOverflow for an example, perhaps it applies to your case.