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 6765723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:44:44+00:00 2026-05-26T14:44:44+00:00

I’m trying to replace an existing serializer with protobuf for C# by Marc Gravell.

  • 0

I’m trying to replace an existing serializer with protobuf for C# by Marc Gravell. My code is extensive and my goal is to be able to do the switch with minimal changes.

I came across an issue which I believe I understand why it happens but require help with overcoming – particularly a solution that would require the least changes in my already existing code and classes. My code is complex so I created the following short sample to demonstrate the issue:

using System;
using System.Collections.Generic;
using System.IO;
using ProtoBuf;


namespace ConsoleApplication1
{
    class program_issue
    {

    [ProtoContract]
    public class Father
    {
        public Father()
        {
            sonny = new Son();
        }

        [ProtoMember(101)]
        public string Name;

        [ProtoMember(102)]
        public Son sonny;

    }

    [ProtoContract]
    public class Son
    {
        public Son()
        {
            Dict.Add(10, "ten");
        }

        [ProtoMember(103)]
        public Dictionary<int, string> Dict = new Dictionary<int, string>();
    }


    static void Main(string[] args)
    {
        Father f1 = new Father();
        f1.Name = "Hello";
        byte[] bts = PBSerializer.Serialize(typeof(Father), f1);

        Father f2;
        PBSerializer.Deserialize(bts, out f2);

    }


    public static class PBSerializer
    {
        public static byte[] Serialize(Type objType, object obj)
        {
            MemoryStream stream = new MemoryStream();
            ProtoBuf.Serializer.Serialize(stream, obj);
            string s = Convert.ToBase64String(stream.ToArray());
            byte[] bytes = stream.ToArray();
            return bytes;
        }


        public static void Deserialize(byte[] data, out Father obj)
        {
            using (MemoryStream stream = new MemoryStream(data))
            {
                obj = ProtoBuf.Serializer.Deserialize<Father>(stream);
            }

        }
    }

}
}

In short, when the parent object is created, it creates a son object which inits a dictionary with some values. I assume that when protobuf tries to rebuild the object when deserializing it uses the same constructor (thus also initiating the dictionary with values) and then tries to push the same values again as part of the deserializing -> error.

How can I overcome it with minimal changes to my code?

Kind regards,
Yossi.

  • 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-26T14:44:45+00:00Added an answer on May 26, 2026 at 2:44 pm

    The easiest option here is probably:

    [ProtoContract(SkipConstructor = true)]
    

    which will, as it says, not execute the constructor (or field-initializers). Note that this will leave the dictionary null if there is no data. Another approach might be to use a serialization callback (which fires just before it gets loaded with data):

    [ProtoBeforeDeserialization]
    private void Foo()
    {
        Dict.Clear();
    }
    

    A third option would be to combine the above by using:

    [ProtoContract(SkipConstructor = true)]
    

    and:

    [ProtoAfterDeserialization]
    private void Foo()
    {
        if(Dict == null) Dict = new Dictionary<int,string>();
    }
    

    to default it to an empty dictionary even if there was no data. Note you would need to do this from Father too, since that uses the default Son constructor.

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

Sidebar

Related Questions

I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string

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.