DataContractSerializer 跳过 OpenAccess 版本的值
我正在使用 OpenAccess 断开连接的模型。 当我尝试使用 DataConractSerializer 反序列化对象时,该对象的 Version 属性为 0 - 但仅在 xml 中。 如果我调试代码并观察该值 - 它是 1 (或 2,3...)
如果我在序列化之前说“int temp = object.Version”,seriazlier 可以保存该值。
我确信有一个懒惰的问题。 如何在不显式调用的情况下强制读取/保存该值?
I'm using OpenAccess disconnected model. When I try to deserialize an object with DataConractSerializer, the Version property of this object is 0 - but only in the xml. If I debug the code and watch the value - it's 1 (or 2,3...)
If I say before the serialization "int temp = object.Version" the seriazlier can save the value.
There is a lazy issue, I'm sure. How can I force to read/save this value without explicit calls?
Code snippets: http://www.telerik.com/community/forums/orm/general-discussions/objectnetworkattacher-vs-datacontractserializer.aspx#775451
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该在对象上调用 .Retrieve() 方法,该方法将加载所有延迟加载的字段,并且您将拥有序列化所需的所有数据。 希望有帮助。
you should call the .Retrieve() method on your object which will load all the lazy-loaded fields and you will have all the data required for serialization. Hope that helps.
听起来
[DataMember]
是针对字段设置的(或默认为字段),并且绕过了惰性行为。 如果您控制类型,您也许可以添加一个[OnSerializing]
方法,在序列化开始之前查看此属性...这样它应该有一个值,并且您不需要将代码到其他地方。如果生成了类型,请查看它是否是
部分类
。 如果是这样,您可以添加另一个同名(和相同命名空间)的部分类
,并将您的[OnSerializing]
方法放入其中...示例(取消注释最后一个阻止使其工作):
It sounds like the
[DataMember]
is set against the field (or is defaulting to fields), and is bypassing the lazy behaviour. If you control the type, you could perhaps add an[OnSerializing]
method that peeks at this property before serialization kicks in... that way it should have a value, and you won't need to put code into other places.If the type is generated, look to see if it is a
partial class
. If so, you can add anotherpartial class
of the same name (and same namespace), and put your[OnSerializing]
method in there...Example (uncomment the last block to make it work):