Protobuf-net possible recursion detected: serialize children and parents

柔情痞子 提交于 2019-12-23 11:58:06

问题


I am new to serialization in general, and even newer to protobuf. Here is my problem, I have these classes:

[ProtoContract]
class Controle
{
    [ProtoMember(1, AsReference=true)]
    public HashSet<Controle> ControlesInternes { get; set; }
    [ProtoMember(2)]
    public string TypeControle { get; set; }
    [ProtoMember(3)]
    public Dictionary<string, string> Attributs { get; set; }
    [ProtoMember(4)]
    public int Ligne { get; set; }
    [ProtoMember(5)]
    public string InnerText { get; set; }
    [ProtoMember(6)]
    public Controle Parent { get; set; }

    public Controle()
    {
        ControlesInternes = new HashSet<Controle>();
        Attributs = new Dictionary<string, string>();
    }
}

[ProtoContract(SkipConstructor=true)]
class PageAspx
{

    [ProtoMember(1)]
    public string PrefixeControleOnilait { get; set; }
    [ProtoMember(2, AsReference = true)]
    public HashSet<Controle> Controles { get; set; }

    private string CheminTmp;

    private string nomFichier;

    [ProtoMember(3)]
    public string NomFichier
    {
        get { return nomFichier; }
        set { nomFichier = value; }
    }

    private string titre;
    [ProtoMember(4)]
    public string Titre
    {
        get { return titre; }
        set { titre = value; }
    }


    public PageAspx()
    { }

    public PageAspx(string pNomFichier)
    {
        this.NomFichier = pNomFichier;

        this.Controles = new HashSet<Controle>();
    }
}

When trying to serialize, I get a "possible recursion detected" error.

But basically, my code lists all controls in an aspx page, and they hierarchy (children, parents). That means that after my "PageAspx" object is made, it contains all the controls of the page, and for each of them, its parent and its children if it has any. When I don't serialize the member ControlesInternes, the serialization goes well. But I need this information.

How can I save these datas using protobuf?


回答1:


I found a solution: I don't serialize the parents, and I use this function after deserialization in the "Controle" class:

    [ProtoAfterDeserialization]
    protected void OnDeserialized()
    {
        if (ControlesInternes.Count > 0)
        {
            foreach (var ctl in ControlesInternes)
            {
                ctl.Parent = this;
            }
        }
    }


来源:https://stackoverflow.com/questions/12142969/protobuf-net-possible-recursion-detected-serialize-children-and-parents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!