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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:05:35+00:00 2026-05-11T02:05:35+00:00

I encountered a, at least to my expectations, strange behavior in the binary serialization

  • 0

I encountered a, at least to my expectations, strange behavior in the binary serialization of .NET.

All items of a Dictionary that are loaded are added to their parent AFTER the OnDeserialization callback. In contrast List does the other way. This can be really annoying in real world repository code, for example when you need to add some delegates to dictionary items. Please check the example code and watch the asserts.

Is it normal behaviour?

[Serializable] public class Data : IDeserializationCallback {     public List<string> List { get; set; }      public Dictionary<string, string> Dictionary { get; set; }      public Data()     {         Dictionary = new Dictionary<string, string> { { 'hello', 'hello' }, { 'CU', 'CU' } };         List = new List<string> { 'hello', 'CU' };     }      public static Data Load(string filename)     {         using (Stream stream = File.OpenRead(filename))         {             Data result = (Data)new BinaryFormatter().Deserialize(stream);             TestsLengthsOfDataStructures(result);              return result;         }     }      public void Save(string fileName)     {         using (Stream stream = File.Create(fileName))         {             new BinaryFormatter().Serialize(stream, this);         }     }      public void OnDeserialization(object sender)     {         TestsLengthsOfDataStructures(this);     }      private static void TestsLengthsOfDataStructures(Data data)     {         Debug.Assert(data.List.Count == 2, 'List');         Debug.Assert(data.Dictionary.Count == 2, 'Dictionary');     } } 
  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T02:05:36+00:00Added an answer on May 11, 2026 at 2:05 am

    I can reproduce the problem. Had a look around Google and found this: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94265 although I’m not sure it’s the exact same problem, it seems pretty similar.

    EDIT:

    I think that adding this code may have fixed the problem?

        public void OnDeserialization(object sender)     {             this.Dictionary.OnDeserialization(sender);     } 

    No time to exhaustively test, and I want to beat Marc to the answer 😉

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

Sidebar

Related Questions

I encountered a problem when running some old code that was handed down to
I encountered a strange problem today. Whenever i put a breakpoint in one of
in the past we encountered various memory-leaks in our software. We found out that
On our SQL Server 2005 machine, I encountered a bug where all the 'f'
I encountered a weird behavior of Cocoa Touch / iOS involving static method. If
I have encountered a very weird behavior while using SimpleDateFormat for parsing a string
Has anyone encountered nested iframes in their Rails/Facebooker application? Basically, every time I click
Well like many other I encountered some problems while uploading my binary to appstore.
When creating scrollable user controls with .NET and WinForms I have repeatedly encountered situations
I have a simple setup and encountered a puzzling (at least for me) problem:

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.