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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:04:50+00:00 2026-05-18T02:04:50+00:00

I am performing serialization using System.Runtime.Serialization.Formatters.Binary.BinaryFormatter . I have a collection which wraps a

  • 0

I am performing serialization using System.Runtime.Serialization.Formatters.Binary.BinaryFormatter. I have a collection which wraps a List. When I deserialize the collection (which deserializes the List), all of the List‘s elements are null if any of the elements implements ISerializable, except those items which I serialize alongside the List. Here’s a test case:


Main

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

static class Program {
    static void Main(string[] args) {
        var collection = new PluginCollection();
        collection.Add(new NullPlugin());
        collection.Add(new NotNullPlugin());

        Clone(collection);
    }

    private static byte[] Serialize(object obj, IFormatter formatter) {
        using (var writer = new MemoryStream()) {
            formatter.Serialize(writer, obj);

            return writer.GetBuffer().Take((int) writer.Length).ToArray();
        }
    }

    private static object Deserialize(byte[] data, IFormatter formatter) {
        using (var stream = new MemoryStream(data)) {
            return formatter.Deserialize(stream);
        }
    }

    public static T Clone<T>(T obj) {
        var cloner = new BinaryFormatter {
            Context = new StreamingContext(StreamingContextStates.Clone)
        };

        return (T) Deserialize(Serialize(obj, cloner), cloner);
    }
}

IPlugin and implementors

interface IPlugin {
}

[Serializable]
sealed class NullPlugin : IPlugin, ISerializable {
    public NullPlugin() {
    }

    private NullPlugin(SerializationInfo info, StreamingContext context) {
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context) {
    }
}

[Serializable]
sealed class NotNullPlugin : IPlugin {
}

PluginCollection

[Serializable]
sealed class PluginCollection : ISerializable {
    private readonly IList<IPlugin> plugins = new List<IPlugin>();

    public PluginCollection() {
    }

    private PluginCollection(SerializationInfo info, StreamingContext context) {
        var plugins = (IEnumerable<IPlugin>) info.GetValue("Plugins", typeof(IEnumerable<IPlugin>));
        //var plugin = (IPlugin) info.GetValue("Plugin", typeof(IPlugin));

        System.Diagnostics.Debug.Assert(!plugins.Any((p) => p == null));

        AddRange(plugins);
        //Add(plugin);
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context) {
        info.AddValue("Plugins", this.plugins);
        //info.AddValue("Plugin", this.plugins.First());
    }

    public void Add(IPlugin plugin) {
        if (plugin == null) {
            throw new ArgumentNullException("plugin");
        }

        this.plugins.Add(plugin);
    }

    private void AddRange(IEnumerable<IPlugin> plugins) {
        if (plugins == null) {
            throw new ArgumentNullException("plugins");
        }

        foreach (var plugin in plugins) {
            Add(plugin);
        }
    }
}

PluginCollection is the (stripped-down) collection with the List. NullPlugin is an IPlugin which implements ISerializable, and NotNullPlugin is an IPlugin which does not.

  1. Leaving the code alone, the deserialized collection its { null, null }.
  2. Uncommenting the lines in PluginCollection causes NullPlugin to be successfully deserialized, while NotNullPlugin is still not deserialized. The collection is { NullPlugin, null }.
  3. Commenting the collection.Add(new NullPlugin()); line in Main causes NotNullPlugin to be successfully deserialized. The collection is { NotNullPlugin }.

  4. When NullPlugin is not successfully serialized, the private deserialization constructor is never called.

  5. Serializing the List directly (e.g. by making PluginCollection.plugins public and cloning collection.plugins in Main) produces the expected results (i.e. deserialization is successful and there are no null elements in the returned list).

I expect the collection of the current code to return { NullPlugin, NotNullPlugin }, and I see no reason why it shouldn’t. What could be causing the failure?

(This problem occurs outside of directly cloning the object, but cloning is the easiest way to show this bug.)

  • 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-18T02:04:50+00:00Added an answer on May 18, 2026 at 2:04 am

    The first thing I see is the assumption that SerializationInfo.GetValue would return the initialized list when called in the deserialization constructor. This is not the case, you’re only receiving an reference to a list that will be populated when everything’s done. You may want to use the IDeserializationCallback interface which triggers when the entire object graph has been deserialized.

    What if the list contains your instance, how would it be fully instantiated before calling your constructor?

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

Sidebar

Related Questions

I have a class that I serialize/deserialize using XmlSerializer . This class contains a
Greetings, What is the most idiomatic way of performing serialization/deserialization of binary custom formats?
I have a javascript routine that is performing actions on a group of checkboxes,
During performing some performance tuning in my application I have noticed, that hibernate query
i am performing search condition of image file & using print_r() it shows output
assume that we are performing search using keywords: keyword1, keyword2, keyword3 there are records
Performing user authentication in Java EE / JSF using j_security_check I tried this solution.
When performing a factor analysis using factanal the usual result is some loadings table
I'm performing a thought-experiment which, judging by other questions, isn't so novel after all,
I noticed when performing many database transactions using BeginSaveChanges(null, null) the last query is

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.