Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7527105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:01:39+00:00 2026-05-30T04:01:39+00:00

I’m having a recursion issue in this code: using System; using System.Collections.Generic; using System.Linq;

  • 0

I’m having a recursion issue in this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using ProtoBuf;

namespace ConsoleApplication4
{
[Serializable]
[ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
public class GeneralWrapper<T> where T : new()
{
    public GeneralWrapper()
    {
        Datas = new T();
    }

    public T Datas { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        List<Document> _documents = new List<Document>();

        for (int i = 0; i < 100000; i++)
        {
            Document _document = new Document()
            {
                DocumentName = "asdadsf"
            };

            _documents.Add(_document);
        }

        BinaryFormatter _formatter = new BinaryFormatter();

        FileStream fs = new FileStream
            ("Person1.bin", FileMode.OpenOrCreate);

        ProtoBuf.Serializer.Serialize(fs, _documents);

        fs.Close();

        // Deserialize.
        fs = new FileStream
            ("Person1.bin", FileMode.OpenOrCreate);

        List<Document> _document22 = ProtoBuf.Serializer.Deserialize<List<Document>>(fs);

        fs.Close();
    }
}

[ProtoContract]
public class Document
{
    public Document()
    {
        _count = 234234924;

        Section = new Section();

        Section.SectionName = "sdfasd";
    }

    [ProtoMember(1)]
    public string DocumentName { get; set; }

    [ProtoMember(2)]
    Dictionary<string, List<string>> Hello { get; set; }
    [ProtoMember(3, AsReference=true)]
    public Section Section { get; set; }

    [ProtoMember(4)]
    private string _sectionName;
    [ProtoMember(5)]
    public string SectionName
    {
        get
        {
            return Section.SectionName;
        }

        set
        {
            _sectionName = value;

            Section.SectionName = _sectionName;
        }
    }

    public int _count;
    public int Count
    {
        get
        {
            return _count;
        }
    }
}

[Serializable]
[ProtoContract]
public class Section
{
    public Section()
    {
        Section1 = new SectionInner(this);

        Hellos = new List<GeneralWrapper<List<string>>>();

        GeneralWrapper<List<string>> _hello = new GeneralWrapper<List<string>>();

        _hello.Datas.Add("hello");

        Hellos.Add(_hello);

        DHello = new Dictionary<string, List<Section>>();

        if (!DHello.ContainsKey("asdf"))
        {
            List<Section> _dhello1 = new List<Section>();

            _dhello1.Add(this);

            DHello.Add("asdf", _dhello1);
        }
    }

    [ProtoMember(1, AsReference=true)]
    public SectionInner Section1 { get; set; }
    [ProtoMember(2)]
    public string SectionName { get; set; }

    [ProtoMember(3, AsReference=true)]
    public Dictionary<string, List<Section>> DHello { get; set; }

    List<GeneralWrapper<List<string>>> Hellos { get; set; }
}

[Serializable]
[ProtoContract]
public class SectionInner
{
    public SectionInner(Section section)
    {
        Section = section;
    }

    [ProtoMember(1, AsReference=true)]
    public Section Section { get; set; }
}

}

I obviously made the code very recursive to begin with because it is the same thing my real project is doing. The problem seems to be this:

 Dictionary<string, List<Section>>

When nothing is added to this dictionary, everything serializes fine. If a list is added to the dictionary with a certain key, recursion happens.

Is this code/syntax supported in protobuf-net?:

 Dictionary<string, List<Section>>

Do I need to put the List in a outer wrapper class like:

 Dictionary<string, Wrapper<List<Section>>>

Thanks for your help. I’m new to protobuf-net.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:01:43+00:00Added an answer on May 30, 2026 at 4:01 am

    Firstly – I must note that performing that much setup in the constuctors is not a good idea. If that is indicative of your actual code , you might want to skip the constructors during deserialization ([ProtoContract(SkipConstructor=true)]). If it is just illustrative, fine.

    Yes, things like dictionaries and lists are supported, however *directly nested lists are not – so a List<List<...>> is not ok at the moment. You’ll probably get away with it with a Dictionary<TKey,List<...>> because its he key-value-pair already acts as a wrapper in the middle.

    Re recursion: protobuf-net has support for many recursion scenarios, but this is as an extension to the formal specification. As such, you need to explicitly enable it, and note: it won’t be easy to interop this scenario, since there is no formal specification for this; but: [ProtoMember(n, AsReference=true)] enables object tracking on an individual member. Note that all places that use this object must be marked as such, else they will use tree serialization.

    Re “why isn’t recursion supported directly” – because: protobuf (the formal spec) behaves like most serializers here, and is a tree serializer. Note: things like XmlSerializer, JavascriptSerializer and DataContractSerializer (in default mode) are also tree serializers, and will blow up if given a recursive structure. This is normal. protobuf-net goes out of its way to allow this in a few scenarios, but can’t enable it by default since it would require a different data layout, defeating the purpose of a cross-platform data specification.

    If I’ve missed any of your questions, please say.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have this code to decode numeric html entities to the UTF8 equivalent character.
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.