如何建模“懒惰” Protobuf-net中的字段

发布于 2025-01-22 15:55:17 字数 822 浏览 3 评论 0原文

我正在使用Protobuf-net进行序列化。我有一个包含“懒惰”字段的课程,如何进行设置并使测试案例工作?任何帮助将受到高度赞赏。

[Fact]
public void LazyField()
{
    MetaType metaType = RuntimeTypeModel.Default.Add(typeof(WithLazyField), true);
    metaType.UseConstructor = false;
    ValueMember metaField = metaType.AddField(1, "_lazyField");
    metaField.AsReference = true;

    var obj = new WithLazyField();

    var clone = Serializer.DeepClone(obj);
    Assert.Equal(obj.GetValue(), clone.GetValue());
}

public class WithLazyField
{
    private readonly Lazy<double> _lazyField;

    public WithLazyField()
    {
        _lazyField = new Lazy<double>(() => Calculate());
    }

    public double Calculate()
    {
        return 1.0;
    }

    public double GetValue()
    {
        return _lazyField.Value;
    }
}

谢谢

I'm using protobuf-net for serialization. I have a class which contains a "Lazy" field, how to set it up and make the test case work? Any help is highly appreciated.

[Fact]
public void LazyField()
{
    MetaType metaType = RuntimeTypeModel.Default.Add(typeof(WithLazyField), true);
    metaType.UseConstructor = false;
    ValueMember metaField = metaType.AddField(1, "_lazyField");
    metaField.AsReference = true;

    var obj = new WithLazyField();

    var clone = Serializer.DeepClone(obj);
    Assert.Equal(obj.GetValue(), clone.GetValue());
}

public class WithLazyField
{
    private readonly Lazy<double> _lazyField;

    public WithLazyField()
    {
        _lazyField = new Lazy<double>(() => Calculate());
    }

    public double Calculate()
    {
        return 1.0;
    }

    public double GetValue()
    {
        return _lazyField.Value;
    }
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

转瞬即逝 2025-01-29 15:55:17

可以通过替代支持“懒惰”类型

The "Lazy" type can be supported through surrogate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文