序列化图形时出现异常 [英] exception while serializing a graph
问题描述
我仍在使用新发布的 protobuf-net 版本,但遇到了一个我不明白的问题.
I'm still playing with the newly released version of protobuf-net and I'm facing an issue I don't understand.
让我们考虑下面的一段代码:
let's consider the piece of code below:
[ProtoContract]
class Node
{
public Node()
{
Children = new List<Node>();
}
[ProtoMember(1, IsRequired = true)]
public int Data { get; set; }
[ProtoMember(2, IsRequired = true, AsReference = true)]
public List<Node> Children { get; set; }
public void AddChild(Node child)
{
Children.Add(child);
}
}
static void Main()
{
Node n = new Node {Data = 0}, root = n;
for (int i=1; i<15; i++)
{
Node child = new Node {Data = i};
n.AddChild(child);
n = child;
}
Node clone = Serializer.DeepClone(root);
}
它抛出一个 ProtoException
类型的异常,消息检测到可能的递归......"
It throws a exception of type ProtoException
with the message "Possible recursion detected..."
有趣的是,如果我删除 Children
属性上的 AsReference
属性,它不会!不幸的是,上面的行只是为了说明问题而编写的,我需要这个属性用于我正在使用的真实结构.
The funny thing is that if I remove the attribute AsReference
on the Children
property it doesn't! Unfortunately, the lines above are just written to illustrate the problem and I need this attribute for the real structure I'm using.
所以我的问题是...这是一个已知问题吗?是否有任何补丁计划很快修复它?或者有人知道任何解决方法吗?
So my question is... is it a known problem and is there any patch planned to fix it very soon? Or does anyone know any workaround?
谢谢
推荐答案
这只是一个错误(感谢您如此彻底地练习测试版!) - 在动态/引用处理中,它重复计算了对象(曾经作为它欺骗了 shim 包装器的一部分来实现魔法,还有一次是为了对象本身).
This is simply a bug (thank you for exercising the beta so thoroughly!) - in the dynamic/reference handling it was double-counting the object (once as part of the shim wrapper it spoofs to do the magic, and once for the object itself).
为了提高效率,递归检测仅在超过特定深度时才启动.您的代码绊倒了这个深度,导致重复计数被视为递归.我已经在代码中修复了这个问题.上面的代码在本地通过,会在下一个drop中.
For efficiency, the recursion detection only kicks into full gear beyond a specific depth. Your code tripped this depth, causing the double-counting to be seen as recursion. I have fixed this in the code. The code above passes locally, and will be in the next drop.
这篇关于序列化图形时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!