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

  • Home
  • SEARCH
  • 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 8290653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:52:14+00:00 2026-06-08T12:52:14+00:00

What’s wrong with the following code? MyList l = Serializer.DeepClone(new MyList { abc });

  • 0

What’s wrong with the following code?

MyList l = Serializer.DeepClone(new MyList { "abc" });
Console.WriteLine(l.Count == 1);

Where:

[ProtoContract]
public class MyList : List<String>
{
}

Expected: true, actual: false.

  • 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-08T12:52:16+00:00Added an answer on June 8, 2026 at 12:52 pm

    If you want an immediate fix, just take away the [ProtoContract]; lists have inbuilt behaviour, and don’t need to be marked as contracts.

    It looks like in this case the contract definition is taking precedence over the fact that it is a list; I will need to look carefully to see if changing this causes any unwanted side-effects.

    Working example:

    public class MyList : List<string>{}
    
    [Test]        
    public void ListSubclassShouldRoundTrip()
    {
        var list = new MyList { "abc" };
        var clone = Serializer.DeepClone(list);
        Assert.AreEqual(1, clone.Count);
        Assert.AreEqual("abc", clone[0]);
    }
    

    currently broken example:

    [ProtoContract]
    public class MyContractList : List<string> { }
    
    [Test]
    public void ContractListSubclassShouldRoundTrip()
    {
        var list = new MyContractList { "abc" };
        var clone = Serializer.DeepClone(list);
        Assert.AreEqual(1, clone.Count);
        Assert.AreEqual("abc", clone[0]);
    }
    

    Update; however, it looks like this should work, since it works fine when the list is a member of a type; the problem only occurs when the list is the outermost type in a graph. Example (all tests pass):

    [ProtoContract]
    public class ListWrapper
    {
        [ProtoMember(1)]
        public List<string> BasicList { get; set; }
        [ProtoMember(2)]
        public MyList MyList { get; set; }
        [ProtoMember(3)]
        public MyContractList MyContractList { get; set; }
    }
    
    [Test]
    public void TestBasicListAsMember()
    {
        var obj = new ListWrapper { BasicList = new List<string> { "abc" } };
        var clone = Serializer.DeepClone(obj);
        Assert.IsNull(clone.MyList);
        Assert.IsNull(clone.MyContractList);
        Assert.AreEqual(1, clone.BasicList.Count);
        Assert.AreEqual("abc", clone.BasicList[0]);
    }
    
    [Test]
    public void TestMyListAsMember()
    {
        var obj = new ListWrapper { MyList = new MyList { "abc" } };
        var clone = Serializer.DeepClone(obj);
        Assert.IsNull(clone.BasicList);
        Assert.IsNull(clone.MyContractList);
        Assert.AreEqual(1, clone.MyList.Count);
        Assert.AreEqual("abc", clone.MyList[0]);
    }
    
    [Test]
    public void TestMyContractListAsMember()
    {
        var obj = new ListWrapper { MyContractList = new MyContractList { "abc" } };
        var clone = Serializer.DeepClone(obj);
        Assert.IsNull(clone.BasicList);
        Assert.IsNull(clone.MyList);
        Assert.AreEqual(1, clone.MyContractList.Count);
        Assert.AreEqual("abc", clone.MyContractList[0]);
    }
    

    Update update: this was a subtle bug, caused by the many different ways that lists can behave; in the case of lists as members that have getters and setters, it has some logic that says “if we get the list, and it wasn’t null, don’t call the setter – just add”. However, the code that handles types-that-are-lists was accidentally enabling that mode. So what was happening was: it would deserialize the data, then proclaim “you can discard this value” – which it dutifully did. Then the “never return null” code would just create a new list, and return that instead. Annoyingly, just a one-liner to fix, and no side-effects; fixed in r555.

    Whenever someone gets stuck, I add an integration test; so this shouldn’t happen again

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

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.