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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:25:02+00:00 2026-05-24T22:25:02+00:00

I am trying out the AsReference option in ProtoMember for recursive referencing. If I

  • 0

I am trying out the AsReference option in ProtoMember for recursive referencing. If I create AnOwner with the public constructor and then serialize/deserialize, AnOwner.Data becomes null. Can someone explain what happens internally and whether recursive referencing is supported? Thanks!

[ProtoContract()]
public class SomeData
{
    [ProtoMember(1, AsReference = true)]
    public AnOwner Owner;

    [ProtoMember(2)]
    public string Value;

    /// <summary>
    /// ProtoBuf deserialization constructor. In fact, Serializer did not complain when this is missing
    /// </summary>
    private SomeData()
    {

    }

    public SomeData(string value)
    {
        Value = value;
    }
}

[ProtoContract()]
public class AnOwner
{
    [ProtoMember(1)]
    public SomeData Data;

    /// <summary>
    /// ProtoBuf deserialization constructor
    /// </summary>
    private AnOwner()
    {

    }

    public AnOwner(SomeData data)
    {
        Data = data;
        Data.Owner = this;
    }
}

EDIT:
After much deliberation, I manage to understand it in the form of this small demo which I will share here. With the current implementation (v2 beta) it matters if AsReference=true is specified for both, neither or either with respect to which object is passed into Serializer.Serialize().

public class Program
{
    using System.IO;
    using ProtoBuf;
    using System;

    public static void main();
    {
        AnOwner owner1, owner2;
        AnOwner owner = new AnOwner();
        SomeData data = new SomeData();
        owner.Data = data;
        data.Owner = owner;
        string file = "sandbox.txt";

        try { File.Delete(file); } catch {}; // Just in case, cos' it felt like some caching was in place.

        using (var fs = File.OpenWrite(file)) { Serializer.Serialize(fs, owner); }
        using (var fs = File.OpenRead(file)) { owner1 = Serializer.Deserialize<AnOwner>(fs); }
        using (var fs = File.OpenRead(file)) { owner2 = Serializer.Deserialize<AnOwner>(fs); }

        Console.WriteLine("SomeData.i: {0}, {1}, {2}, {3}", owner1.Data.i, owner1.Data.Owner.Data.i, owner2.Data.i, owner2.Data.Owner.Data.i);
        Console.WriteLine("AnOwner.i: {0}, {1}, {2}, {3}", owner1.i, owner1.Data.Owner.i, owner2.i, owner2.Data.Owner.i);

        System.Diagnostics.Debug.Assert(owner1 == owner1.Data.Owner, "1. Expect reference same, but not the case.");
        System.Diagnostics.Debug.Assert(owner2 == owner2.Data.Owner, "2. Expect reference same, but not the case.");
        System.Diagnostics.Debug.Assert(owner1 != owner2, "3. Expect reference different, but not the case.");
    }
}

[ProtoContract()]
public class SomeData
{
    public static readonly Random RAND = new Random(2);

    [ProtoMember(1, AsReference = true)]
    public AnOwner Owner;

    // Prove that SomeData is only instantiated once per deserialise
    public int i = RAND.Next(100);

    public SomeData() { }
}

[ProtoContract()]
public class AnOwner
{
    public static readonly Random RAND = new Random(3);

    [ProtoMember(1, AsReference=true)]
    public SomeData Data;

    // Prove that AnOwner is only instantiated once per deserialise
    public int i = RAND.Next(100);

    /// <summary>
    /// ProtoBuf deserialization constructor
    /// </summary>
    public AnOwner() { }
}
  • 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-24T22:25:04+00:00Added an answer on May 24, 2026 at 10:25 pm

    Basically, instead of serializing AnOwner directly, it serializes a fake (doesn’t really exist) object with one or more of the following:

    • the existing key (integer) to an object that has already been seen
    • a new key
    • the object itself
    • the type information (if DynamicType is enabled)

    When serializing a tracked object, an internal list is checked; if the object is there (has been seen before), then the old key (only) is written. Otherwise a new key is generated and stored against that object, and the new key and the object are written. When deserializing, if a “new key” is found, the object data is deserialized and the new object is stored against that key (actually the order here is a bit complex, to handle recursion). If an “old key” is found, the internal list is used to fetch the old old object.

    For almost all objects, comparison is on a reference equality basis (even if equality is overriden). Note that this works slightly differently for strings, which are compared for string equality – so two different instances of the string "Fred" will still share a key.

    I believe most recursion scenarios are supported, but if you get an issue please let me know.

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

Sidebar

Related Questions

I'm trying out log4j in a simple test app. I create a new Java
I was trying out the jQuery Data linking proposal from Microsoft and noticed something
I'm trying out my first recursive function (at least I think I am!) and
Trying out the examples found on pleac.sf.net , I'm not able to get an
While trying out an experimental UINavigationController-based iPhone application, I ran into a problem when
After trying out VS2010b2 also my VS2008 installation changes the versions of solution and
Just trying out Hibernate (with Annotations) and I'm having problems with my mappings. I
im trying out paypals html api where you specify price, item_name, customer information and
I'm currently trying out db4o (the java version) and I pretty much like what
I'm trying out the following query: SELECT A,B,C FROM table WHERE field LIKE 'query%'

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.