使用 protobuf.net 序列化图表时出现问题

发布于 2024-11-09 19:54:25 字数 1516 浏览 0 评论 0原文

在使用新发布的 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 技术交流群。

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

发布评论

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

评论(1

我仍然需要评估影响(当我的头感觉像奶酪时,这不是干的事情),但是图中的 root 对象有一个......麻烦点 - 意思是:因为我通常在遍历关联时处理引用跟踪,并且在根对象上没有关联。我想我可以用一些类型级属性来解决这个问题(即总是将其视为参考)。

不管怎样,现在你可以通过在图表中添加一个级别(人为地添加关联)来回避这个问题,即

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

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