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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:32:00+00:00 2026-06-06T00:32:00+00:00

I’m using protobuf-net v2 and have a class that inherits List that I wan’t

  • 0

I’m using protobuf-net v2 and have a class that inherits “List” that I wan’t to serialize/clone.
when I’m calling “DeepClone” (or deserialize) I’m getting the cloned object empty.
I can serialize the object into file and it seems to be serialized as expected but the RuntimeTypeModel can’t deserialized it back from the byte[].

the single solution I’ve found to overcome this issue is to use surrogate.

as said, if you’re skipping the “SetSurrogate” the clone is failed.
is there any other option to solve it?

attached:

class Program
{
    static void Main(string[] args)
    {
        RuntimeTypeModel model = RuntimeTypeModel.Create();
        model[typeof(Custom<string>)].SetSurrogate(typeof(Surrogate<string>));

        var original = new Custom<string> { "C#" };
        var clone = (Custom<string>)model.DeepClone(original);
        Debug.Assert(clone.Count == original.Count);
    }
}

[ProtoContract(IgnoreListHandling = true)]
public class Custom<T> : List<T> { }

[ProtoContract]
class Surrogate<T>
{
    public static implicit operator Custom<T>(Surrogate<T> surrogate)
    {
        Custom<T> original = new Custom<T>();
        original.AddRange(surrogate.Pieces);
        return original;
    }

    public static implicit operator Surrogate<T>(Custom<T> original)
    {
        return original == null ? null : new Surrogate<T> { Pieces = original };
    }

    [ProtoMember(1)]
    internal List<T> Pieces { get; set; }
}

Another thing I’ve found is when you’re replacing the ProtoContract attribute from the class “Custom” with “System.Serializable” attribute, it deserializing the byte[] as expected even without surrogate.

  • 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-06-06T00:32:01+00:00Added an answer on June 6, 2026 at 12:32 am

    The problem here is simply the fact that you explicitly turned off the list-handling, via:

    [ProtoContract(IgnoreListHandling = true)]
    public class Custom<T> : List<T> { }
    

    Which, as the name suggests and the documentation verifies:

    /// <summary>
    /// If specified, do NOT treat this type as a list, even if it looks like one.
    /// </summary>
    public bool IgnoreListHandling {...}
    

    So: there was nothing useful left to do, as Custom<T> doesn’t have any other data-members to serialize.

    So: if you aren’t using the surrogate, don’t disable list-handling. The main purpose of this option is for edge-cases where something that is intended to be an “object” also has features that make it look temptingly like a list (all protobuf-net needs is IEnumerable[<T>] and a handy Add(T) method).


    [TestFixture]
    public class SO11034791
    {
        [Test]
        public void Execute()
        {
            RuntimeTypeModel model = RuntimeTypeModel.Create();
    
            var original = new Custom<string> { "C#" };
            var clone = (Custom<string>)model.DeepClone(original);
            Assert.AreEqual(1, clone.Count);
            Assert.AreEqual("C#", clone.Single());
        }
        public class Custom<T> : List<T> { }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.