使用 protobuf.net 序列化图形时出现问题 [英] issue while serializing graphs with protobuf.net

查看:43
本文介绍了使用 protobuf.net 序列化图形时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用新发布的 protobuf.net 时,我们遇到了如下代码所示的问题:

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()));
    }
}

断言抛出异常...我们的目标是在 ChildLinks 和 ParentLinks 属性中拥有一个 Link 对象的唯一实例.我们尝试了 AsReference 属性,但没有用...

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 ?

推荐答案

我仍然需要评估影响(不是在我的头感觉像奶酪的时候干的事情),但是有一个......图中的 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.

static class Program
{
    public static void Main()
    {

        Node node = new Node { Data = "parent" };
        node.AddChild(new Node { Data = "child" });
        using (MemoryStream memStream = new MemoryStream())
        {
            Serializer.Serialize(memStream, new NodeWrapper { Root = node });
            memStream.Position = 0;
            Node deserialized = Serializer.Deserialize<NodeWrapper>(memStream).Root;

            Link childLink = deserialized.ChildLinks.Single();
            Debug.Assert(ReferenceEquals(childLink, childLink.Child.ParentLinks.Single()));
        }
    }
}
[ProtoContract]
class NodeWrapper
{
    [ProtoMember(1, AsReference = true, IsRequired = true)]
    public Node Root {get;set;}
}

这篇关于使用 protobuf.net 序列化图形时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆