检测protobuf网可以递归:序列孩子和家长 [英] Protobuf-net possible recursion detected: serialize children and parents

查看:164
本文介绍了检测protobuf网可以递归:序列孩子和家长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般是新来的系列化,甚至更新的版本来protobuf的。这里是我的问题,我有这些类:

  [ProtoContract] 
类CONTROLE
{
[ProtoMember(1,AsReference = TRUE)]
公众的HashSet<&CONTROLE GT; ControlesInternes {搞定;组; }
[ProtoMember(2)]
公共字符串TypeControle {搞定;组; }
[ProtoMember(3)]
公众解释<字符串,字符串> Attributs {搞定;组; }
[ProtoMember(4)]
公众诠释利涅{搞定;组; }
[ProtoMember(5)]
公共字符串的InnerText {搞定;组; }
[ProtoMember(6)]
公共CONTROLE家长{搞定;组; }

公共CONTROLE()
{
ControlesInternes =新的HashSet<&CONTROLE GT;();
Attributs =新词典<字符串,字符串>();
}
}






  [ProtoContract(SkipConstructor =真)] 
类PageAspx
{

[ProtoMember(1)]
公共字符串PrefixeControleOnilait {得到;组; }
[ProtoMember(2 AsReference = TRUE)]
公众的HashSet<&CONTROLE GT; CONTROLES {搞定;组; }

私人字符串CheminTmp;

私人字符串nomFichier;

[ProtoMember(3)]
公共字符串NomFichier
{
{返回nomFichier; }
集合{nomFichier =价值; }
}

私人字符串滴度;
[ProtoMember(4)]
公共字符串滴度
{
{返回滴度; }
集合{滴度=价值; }
}


公共PageAspx()
{}

公共PageAspx(字符串pNomFichier)
{
this.NomFichier = pNomFichier;

this.Controles =新的HashSet<&CONTROLE GT;();
}
}

在试图序列化,我得到一个可能的递归检测的错误。



但基本上,我的代码列出所有的控件在aspx页面,他们的层次结构(子女,父母)。这意味着,我的PageAspx对象作出后,它包含了页面的所有控件,并为他们每个人,其母公司和它的孩子,如果有任何。当我不序列化成员 ControlesInternes ,序列化顺利。但我需要这些信息。



我如何使用protobuf的?


解决方案

我找到了一个解决办法:我不序列化的父母,我在CONTROLE级使用此功能反序列化后:

  [ProtoAfterDeserialization] 
保护无效OnDeserialized()
{
如果(ControlesInternes.Count大于0)
{
的foreach(VAR在CTL ControlesInternes)
{
ctl.Parent =这一点;
}
}
}


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?

解决方案

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

这篇关于检测protobuf网可以递归:序列孩子和家长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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