简体   繁体   English

可序列化的循环参考

[英]ISerializable Circular Reference

I've been trying to understand how to correctly implement a circular reference using the ISerializable interface. 我一直在尝试了解如何使用ISerializable接口正确实现循环引用。 But i have not been able to work out even in a simple form, I have read the explanation here 但是我什至无法以简单的形式解决问题,我已经在这里阅读了说明

But I have not been able to implement it, I've also tried looked for an example to no avail. 但是我无法实现它,我也尝试过寻找一个没有用的例子。 I've checked the documentation on MSDN, but i unable to any reference to how to handle custom serialization with circular references. 我已经检查了MSDN上的文档,但是无法使用循环引用来引用任何有关如何处理自定义序列化的引用。

The simplest form i've been trying with is a doublely linked list. 我一直在尝试的最简单形式是双向链接列表。

Serialize circular reference just need an strategy to serialize the whole object graph nothing more. 序列化循环引用只需要一种策略即可序列化整个对象图。 for double link list you can start from the first node and then just serialize the next one, (previous is already serialized so nothing to do ) then when you wanted to build the list again do the same just set previous node for each one sequentially(recursively) something like this 对于双链接列表,您可以从第一个节点开始,然后序列化下一个,(先前的序列已被序列化,因此无需执行任何操作),然后当您想再次构建列表时,只需依次为每个节点设置前一个节点即可(递归)这样的事情

public class LinkList : ISerializable
{
    public Node First { get; set; }

    public Node Tail { get; set; }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("Firts", First);
    }
    public LinkList(SerializationInfo info, StreamingContext context)
    {
        First = info.GetValue("First", typeof(Node)) as Node;
        First.PrevNode = null;
        //do one one while set the Tail of this class  and LinkList proeprty for each node
    }
}
public class Node : ISerializable
{
    public LinkList LinkList { get; set; }


    public Node(SerializationInfo info, StreamingContext context)
    {
        Name = info.GetString("Name");
        NextNode = info.GetValue("NextNode", typeof(Node)) as Node;
        if(NextNode != null)
            NextNode.PrevNode = this;

    }
  public  Node PrevNode
    {
        get;
        set;
    }
    public Node NextNode
    {
        get;
        set;
    }
    public string Name
    {
        get;
        set;
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("Name", Name);
        info.AddValue("Next", NextNode);

    }
}

One option to get this to work would to be to add an ID field to the the class. 一种可行的选择是在类中添加一个ID字段。 Make a linked list of integers which will tie to the ID of the field, and a readonly linked list property which will be populated based on finding references to the IDs in the linked list. 创建一个将与字段ID绑定的整数链接列表,并创建一个只读链接列表属性,该属性将基于在链接列表中找到对ID的引用而被填充。

The one constraint on this is that each object in the list of IDs must be available when it is deserialized. 对此的一个约束是ID列表中的每个对象在反序列化时都必须可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM