DataContractSerializer 跳过 OpenAccess 版本的值

发布于 2024-07-15 18:03:06 字数 554 浏览 10 评论 0原文

我正在使用 OpenAccess 断开连接的模型。 当我尝试使用 DataConractSerializer 反序列化对象时,该对象的 Version 属性为 0 - 但仅在 xml 中。 如果我调试代码并观察该值 - 它是 1 (或 2,3...)

如果我在序列化之前说“int temp = object.Version”,seriazlier 可以保存该值。

我确信有一个懒惰的问题。 如何在不显式调用的情况下强制读取/保存该值?

代码片段: http://www .telerik.com/community/forums/orm/general-discussions/objectnetworkattacher-vs-datacontractserializer.aspx#775451

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 技术交流群。

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

发布评论

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

评论(2

柒七 2024-07-22 18:03:06

您应该在对象上调用 .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.

明明#如月 2024-07-22 18:03:06

听起来 [DataMember] 是针对字段设置的(或默认为字段),并且绕过了惰性行为。 如果您控制类型,您也许可以添加一个 [OnSerializing] 方法,在序列化开始之前查看此属性...这样它应该有一个值,并且您不需要将代码到其他地方。

如果生成了类型,请查看它是否是部分类。 如果是这样,您可以添加另一个同名(和相同命名空间)的部分类,并将您的[OnSerializing]方法放入其中...

示例(取消注释最后一个阻止使其工作):

using System;
using System.Runtime.Serialization;
using System.Xml;

class Program {
    static void Main() {
        using (XmlWriter writer = XmlWriter.Create(Console.Out)) {
            new DataContractSerializer(typeof(Foo))
                .WriteObject(writer, new Foo());
        }       
    }
}

[DataContract]
partial class Foo {
    [DataMember(Name="Bar")]
    private int? bar;
    public int Bar {
        get {
            if (bar == null) bar = 27; // somthing lazy
            return bar.GetValueOrDefault();
        }
        set { bar = value; }
    }
}
/* UNCOMMENT THIS
partial class Foo {
    [OnSerializing]
    private void BeforeSerialize(StreamingContext ctx) {
        int tmp = Bar;
    }
}
*/

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 another partial class of the same name (and same namespace), and put your [OnSerializing] method in there...

Example (uncomment the last block to make it work):

using System;
using System.Runtime.Serialization;
using System.Xml;

class Program {
    static void Main() {
        using (XmlWriter writer = XmlWriter.Create(Console.Out)) {
            new DataContractSerializer(typeof(Foo))
                .WriteObject(writer, new Foo());
        }       
    }
}

[DataContract]
partial class Foo {
    [DataMember(Name="Bar")]
    private int? bar;
    public int Bar {
        get {
            if (bar == null) bar = 27; // somthing lazy
            return bar.GetValueOrDefault();
        }
        set { bar = value; }
    }
}
/* UNCOMMENT THIS
partial class Foo {
    [OnSerializing]
    private void BeforeSerialize(StreamingContext ctx) {
        int tmp = Bar;
    }
}
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文