如何建模“懒惰” Protobuf-net中的字段
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以通过替代支持“懒惰”类型
The "Lazy" type can be supported through surrogate