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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:00:05+00:00 2026-05-22T12:00:05+00:00

Please, observe the following simple program (based on the example from the protobuf-net project

  • 0

Please, observe the following simple program (based on the example from the protobuf-net project v1 wiki):

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

namespace HelloProtoBuf
{
  [ProtoContract]
  class Person
  {
    [ProtoMember(1)]
    public int Id { get; set; }
    [ProtoMember(2)]
    public string Name { get; set; }
    [ProtoMember(3)]
    public Address Address { get; set; }
  }
  [ProtoContract]
  class Address
  {
    [ProtoMember(1)]
    public string Line1 { get; set; }
    [ProtoMember(2)]
    public string Line2 { get; set; }
  }


  class Program
  {
    static void Main(string[] args)
    {
      var person = new Person
      {
        Id = 12345,
        Name = "Fred",
        Address = new Address
        {
          Line1 = "Flat 1",
          Line2 = "The Meadows"
        }
      };
      var person2 = new Person
      {
        Id = 4553,
        Name = "Nadya",
        Address = person.Address
      };
      var persons = new List<Person> { person, person2 };
      Debug.Assert(ReferenceEquals(persons[0].Address, persons[1].Address));

      using (var file = File.Create("persons.bin"))
      {
        Serializer.Serialize(file, persons);
      }
      List<Person> persons2;
      using (var file = File.OpenRead("persons.bin"))
      {
        persons2 = Serializer.Deserialize<List<Person>>(file);
      }
      Debug.Assert(ReferenceEquals(persons2[0].Address, persons2[1].Address));
    }
  }
}

The second assertion fails. Is this a bug in the protobuf-net implementation or is it that protocol buffers simply does not support object graphs with shared references?

Thanks.

  • 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-22T12:00:06+00:00Added an answer on May 22, 2026 at 12:00 pm

    protocol-buffers itself does not support this – so no, it is not a bug. Indeed, XmlSerializer and DataContractSerializer* would do the same (and probably so would JavaScriptSerializer and JSON.NET).

    However, it is a common request, so this is supported in protobuf-net v2 (basically: I cheat). Just change it to:

        [ProtoMember(3, AsReference=true)]
        public Address Address { get; set; }
    

    (and use the v2 dll that I’m uploading in about 5 minutes, or build from code)


    *=caveat: DataContractSerializer does support references, but only if you use a specific constructor; it is disabled by default

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

Sidebar

Related Questions

Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
Please help! I'm really at my wits' end. My program is a little personal
Please provide tips for effectively using git with svn. What are your best practices?
I'm seeking a behaviour which combines that of VerticalAlign=Stretch and VerticalAlign=Top . Please observe
Please suggest some good resources to start writing Java Web services.
Please read the whole question. I'm not looking for an approach to managing multi-lingual
Please give me the direction of the best guidance on the Entity Framework.
Please bear with me, I'm just learning C++. I'm trying to write my header
Please explain to me why the very last echo statement is blank? I expect
Please excuse the vague title. If anyone has a suggestion, please let me know!

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.