使用 protobuf.net 序列化图表时出现问题
在使用新发布的 protobuf.net 时,我们遇到了以下代码所示的问题:
[ProtoContract]
class Node
{
public Node()
{
ChildLinks = new List<Link>();
ParentLinks = new List<Link>();
}
[ProtoMember(1, IsRequired = true)]
public string Data { get; set; }
[ProtoMember(2, IsRequired = true)]
public List<Link> ChildLinks { get; set; }
[ProtoMember(3, IsRequired = true)]
public List<Link> ParentLinks { get; set; }
public void AddChild(Node child)
{
Link link = new Link { Parent = this, Child = child };
ChildLinks.Add(link);
child.ParentLinks.Add(link);
}
}
[ProtoContract]
class Link
{
[ProtoMember(2, AsReference = true, IsRequired = true)]
public Node Child { get; set; }
[ProtoMember(3, AsReference = true, IsRequired = true)]
public Node Parent { get; set; }
}
public static void Main()
{
Node node = new Node { Data = "parent" };
node.AddChild(new Node { Data = "child" });
using (MemoryStream memStream = new MemoryStream())
{
Serializer.Serialize(memStream, node);
memStream.Position = 0;
Node deserialized = Serializer.Deserialize<Node>(memStream);
Link childLink = deserialized.ChildLinks.Single();
Debug.Assert(ReferenceEquals(childLink, childLink.Child.ParentLinks.Single()));
}
}
断言抛出异常...我们的目标是在 ChildLinks 和 ParentLinks 属性中拥有 Link 对象的唯一实例。我们尝试了 AsReference 属性,但它不起作用...
有谁知道我们如何解决这个问题?
while playing with the newly released protobuf.net, we are encountering the issue illustrated in the code below:
[ProtoContract]
class Node
{
public Node()
{
ChildLinks = new List<Link>();
ParentLinks = new List<Link>();
}
[ProtoMember(1, IsRequired = true)]
public string Data { get; set; }
[ProtoMember(2, IsRequired = true)]
public List<Link> ChildLinks { get; set; }
[ProtoMember(3, IsRequired = true)]
public List<Link> ParentLinks { get; set; }
public void AddChild(Node child)
{
Link link = new Link { Parent = this, Child = child };
ChildLinks.Add(link);
child.ParentLinks.Add(link);
}
}
[ProtoContract]
class Link
{
[ProtoMember(2, AsReference = true, IsRequired = true)]
public Node Child { get; set; }
[ProtoMember(3, AsReference = true, IsRequired = true)]
public Node Parent { get; set; }
}
public static void Main()
{
Node node = new Node { Data = "parent" };
node.AddChild(new Node { Data = "child" });
using (MemoryStream memStream = new MemoryStream())
{
Serializer.Serialize(memStream, node);
memStream.Position = 0;
Node deserialized = Serializer.Deserialize<Node>(memStream);
Link childLink = deserialized.ChildLinks.Single();
Debug.Assert(ReferenceEquals(childLink, childLink.Child.ParentLinks.Single()));
}
}
The assert throws an exception... Our goal here is to have a unique instance of a Link object in the ChildLinks and ParentLinks properties. We tried the AsReference attribute but it didn't work...
Does anyone know how we could fix that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我仍然需要评估影响(当我的头感觉像奶酪时,这不是干的事情),但是图中的 root 对象有一个......麻烦点 - 意思是:因为我通常在遍历关联时处理引用跟踪,并且在根对象上没有关联。我想我可以用一些类型级属性来解决这个问题(即总是将其视为参考)。
不管怎样,现在你可以通过在图表中添加一个级别(人为地添加关联)来回避这个问题,即
I still need to assess the impact (not something to dry doing while my head feels like cheese), but there is a .... trouble-spot with the root object in the graph - meaning: because I usually handle the reference tracking while walking the association, and on the root object there is no association. I guess I can probably address this with some type-level attribute instead (i.e. always treat as a reference).
Anyway, for now you can side-step this by adding one more level to the graph (artificially adding an association), i.e.