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

The Archive Base Latest Questions

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

I understand that AsReference is not supported for lists with protobuf.net, so I have

  • 0

I understand that AsReference is not supported for lists with protobuf.net, so I have attempted a work-around for this limitation. I have created a custom list called SuperList that contains items wrapped in objects of type SuperListItem as follows:

[ProtoContract]
public class SuperList<T> where T : class
{
    [ProtoMember(1)]
    private List<SuperListItem<T>> _items = new List<SuperListItem<T>>();

    public SuperList()
    {
    }

    public int IndexOf(T item)
    {
        int indexOf = -1;
        for (int index = 0; index < _items.Count; index++)
        {
            if (_items[index].Item == item)
            {
                indexOf = index;
                break;
            }
        }
        return indexOf;
    }

    public void Insert(int index, T item)
    {
        _items.Insert(index, new SuperListItem<T>(item));
    }

    public void RemoveAt(int index)
    {
        _items.RemoveAt(index);
    }

    public T this[int index]
    {
        get
        {
            return _items[index].Item;
        }
        set
        {
            _items[index] = new SuperListItem<T>(value);
        }
    }

    public void Add(T item)
    {
        _items.Add(new SuperListItem<T>(item));
    }

    public void Clear()
    {
        _items.Clear();
    }

    public bool Contains(T item)
    {
        bool contains = false;
        foreach (var listItem in _items)
        {
            if (listItem.Item == item)
            {
                contains = true;
                break;
            }
        }
        return contains;
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        for (int index = arrayIndex; index < _items.Count; index++)
            array[index] = _items[index].Item;
    }

    public int Count
    {
        get { return _items.Count; }
    }

    public bool IsReadOnly
    {
        get { return false; }
    }

    public bool Remove(T item)
    {
        SuperListItem<T> itemToRemove = null;
        foreach (var listItem in _items)
        {
            if (listItem.Item == item)
            {
                itemToRemove = listItem;
                break;
            }
        }
        if (itemToRemove != null)
            _items.Remove(itemToRemove);

        return itemToRemove != null;
    }

    public IEnumerator<T> GetEnumerator()
    {
        foreach(var listItem in _items)
            yield return listItem.Item;
    }
}

[ProtoContract]
public class SuperListItem<T>
{
    [ProtoMember(1, AsReference = true)]
    private readonly T _item;
    public T Item { get { return _item; } }

    private SuperListItem() { }

    public SuperListItem(T item)
    {
        _item = item;
    }
}

I have the following test code for the serialization:

[ProtoContract]
public class Thing
{
    [ProtoMember(1)]
    private readonly string _name;
    public string Name { get { return _name; } }

    private Thing() { }

    public Thing(string name)
    {
        _name = name;
    }
}

public class ProtoTest3
{
    public void Serialize()
    {
        SuperList<Thing> list = GetListOfThings();

        using (var fs = File.Create(@"c:\temp\things.bin"))
        {
            ProtoBuf.Serializer.Serialize(fs, list);

            fs.Close();
        }

        using (var fs = File.OpenRead(@"c:\temp\things.bin"))
        {
            list = ProtoBuf.Serializer.Deserialize<SuperList<Thing>>(fs);

            Debug.Assert(list[0] == list[2]);

            fs.Close();
        }
    }

    private SuperList<Thing> GetListOfThings()
    {
        var thing1 = new Thing("thing1");
        var thing2 = new Thing("thing2");

        var list = new SuperList<Thing>();
        list.Add(thing1);
        list.Add(thing2);
        list.Add(thing1);

        return list;
    }
}

However, when I run the code, it I get the exception “No parameterless constructor defined for this object”. Is it simply a limitation in ProtoBuf.Net, or have I done something wrong. Is there a way around this problem?

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

    To clarify; the limitation on lists is simply that the list itself is not treated as a reference. The items are, as long as they are on a member marked AsReference – for example:

        [Test]
        public void SerializeTheEasyWay()
        {
            var list = GetListOfThings();
    
            using (var fs = File.Create(@"things.bin"))
            {
                ProtoBuf.Serializer.Serialize(fs, list);
    
                fs.Close();
            }
    
            using (var fs = File.OpenRead(@"things.bin"))
            {
                list = ProtoBuf.Serializer.Deserialize<MyDto>(fs);
    
                Assert.AreEqual(3, list.Things.Count);
                Assert.AreNotSame(list.Things[0], list.Things[1]);
                Assert.AreSame(list.Things[0], list.Things[2]);
    
                fs.Close();
            }
        }
    
        [ProtoContract]
        public class MyDto
        {
            [ProtoMember(1, AsReference = true)]
            public List<Thing> Things { get; set; }
        }
    
        private MyDto GetListOfThings()
        {
            var thing1 = new Thing("thing1");
            var thing2 = new Thing("thing2");
    
            var list = new List<Thing>();
            list.Add(thing1);
            list.Add(thing2);
            list.Add(thing1);
    
            return new MyDto {Things = list};
        }
    

    (and fun fact – this is actually exactly the same on the wire)

    There does, however, seem to be a bug with how it creates the instance in this case; it is using the .ctor and failing. I will investigate and fix this, however the following also work:

    1: make the parameterless .ctor public:

            public Thing()
            {
            }
    

    2: or alternatively, disable the .ctor:

        [ProtoContract(SkipConstructor = true)]
        public class Thing
        { ...
    

    I will investigate why private parameterless constructors aren’t happy in this scenario.

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

Sidebar

Related Questions

I understand that this response clearly states that this is not possible without a
I understand from this post that value types in C# are not objects. That
I understand that some countries have laws regarding website accessibility. In general, what are
I understand that Microsoft uses this template when versioning their products: Major.Minor.Build.Revision. Major is
I understand that IronPython is an implementation of Python on the .NET platform just
I understand that these methods are for pickling/unpickling and have no relation to the
I understand that you must copy blocks in order for them to stick around
I understand that the page load of asp.net (mvc 2.0ish) sites can suffer for
I understand that this is a reference for the caller object. I'm used to
I understand that a IQueryable cannot be serialized. That means that queries can not

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.