检测到Protobuf

编程入门 行业动态 更新时间:2024-10-18 12:33:38
检测到Protobuf-net可能的递归:序列化儿童和父母(Protobuf-net possible recursion detected: serialize children and parents)

我是序列化的新手,甚至是protobuf的新手。 这是我的问题,我有这些课程:

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

尝试序列化时,我收到“可能检测到递归”错误。

但基本上,我的代码列出了aspx页面中的所有控件,并且它们是层次结构(子级,父级)。 这意味着在我的“PageAspx”对象生成之后,它包含页面的所有控件,并且对于每个控件都包含其父级及其子级(如果有)。 当我没有序列化成员ControlesInternes ,序列化进展顺利。 但我需要这些信息。

如何使用protobuf保存这些数据?

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?

最满意答案

我找到了一个解决方案:我没有序列化父项,我在“Controle”类中反序列化后使用了这个函数:

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

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

更多推荐

本文发布于:2023-08-06 00:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1440974.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:检测到   Protobuf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!