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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:56:07+00:00 2026-05-28T04:56:07+00:00

As MSDN states here , it can. But I’ve spent 2 hours digging mscorlib

  • 0

As MSDN states here, it can.
But I’ve spent 2 hours digging mscorlib code, because in some cases the BinaryFormatter called my method marked with OnDeserialized BEFORE deserialization constructor. That is, the order was

OnDeserializing(StreamingContext context)
OnDeserialized(StreamingContext context)
.ctor(SerializationInfo info, StreamingContext context)

While I was expecting it to be

OnDeserializing(StreamingContext context)
.ctor(SerializationInfo info, StreamingContext context)
OnDeserialized(StreamingContext context)

And the final point. When I implemented IDeserializationCallback interface, its method OnDeserialization was called AFTER constructor, as I wanted and expected.

I tried to reproduce this on some simple class structure, but there everything worked fine.
In our project the objects graph being serialized is very complex, so I do not know where to dig. Inspecting the mscorlib code with reflector did not help a lot – the deserialization code is too complicated for me to figure out where the problem comes from.

So, does anybody know what could be causing such problem? We use the assumption that OnDeserialized is called BEFORE the constructor in several other places so I am scared now that it is not very reliable…

Thanks!

  • 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-28T04:56:08+00:00Added an answer on May 28, 2026 at 4:56 am

    Finally, I have the answer to my own question, if anyone would be interested.
    Consider the example in the end of this post. There are two classes, instances of which contain references to each other. Under such conditions there is no possibility that deserializing constructors of both instances are passed with constructed objects. So serializer first calls one of constructors passing it an unconstructed instance of second type and then calls constructor of that object, passing it constructed instance of first type. In such a way it helps us to restore objects connections, so it is really the best it can do!

    Next, OnDeserializing and OnDeserialized callbacks in such cases may be called as I pointed in the question, while OnDeserialization method of IDeserializationCallback is always called after the COMPLETE objects graph has been deserialized, exactly as it is stated in its specification.

    Keeping all the above in mind, I find it the best to use IDeserializationCallback interface to do any post-deserialization processing I need. In that case I am sure that constructors are called for all objects and I can do necessary modifications in a ‘safe’ way.

          [Serializable]
          class One :ISerializable, IDeserializationCallback
          {
               public Two m_two;
               public One() {}
               public One(SerializationInfo info, StreamingContext context)
               {
                    var two = (Two)info.GetValue("m_two", typeof(Two));
                    m_two = two;
               }
               public void GetObjectData(SerializationInfo info, StreamingContext context)
               {
                    info.AddValue("m_two", m_two);
               }
               private bool m_onDeserializing;
               private bool m_onDeserialized;
               private bool m_callback;
               public void OnDeserialization(object sender)
               {
                    m_callback = true;
               }
               [OnDeserializing]
               void OnDeserializing(StreamingContext context)
               {
                    m_onDeserializing = true;
               }
    
               [OnDeserialized]
               void OnDeserialized(StreamingContext context)
               {
                    m_onDeserialized = true;
               }
          }
    
          [Serializable]
          private class Two : ISerializable, IDeserializationCallback
          {
               public Two(){}
               public One m_one;
               public Two(SerializationInfo info, StreamingContext context)
               {
                    var one = (One)info.GetValue("m_one", typeof(One));
                    m_one = one;
               }
               public void GetObjectData(SerializationInfo info, StreamingContext context)
               {
                    info.AddValue("m_one", m_one);
               }
               private bool m_onDeserializing;
               private bool m_onDeserialized;
               private bool m_callback;
               public void OnDeserialization(object sender)
               {
                    m_callback = true;
               }
               [OnDeserializing]
               void OnDeserializing(StreamingContext context)
               {
                    m_onDeserializing = true;
               }
               [OnDeserialized]
               void OnDeserialized(StreamingContext context)
               {
                    m_onDeserialized = true;
               }
          }
    
          [STAThread]
          static void Main()
          {
               var one = new One();
               one.m_two = new Two();
               one.m_two.m_one = one;
    
               BinaryFormatter formatter = new BinaryFormatter();
               MemoryStream mss =new MemoryStream();
               formatter.Serialize(mss, one);
               mss.Position = 0;
               var deserialize = formatter.Deserialize(mss);
          }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This article on MSDN states that you can use as many try catch blocks
Having nicked some code from msdn I'm miffed that it doesn't work exactly as
MSDN states that String.Intern retrieves the system's reference to the specified String and String.IsInterned
MSDN states the following SortedSet(T).Add Method : If Count is less than the capacity
The MSDN states that the method returns true if the method is successfully queued;
When does script added to the page with Page.ClientScript.RegisterStartupScript() actually run? MSDN states "when
The MSDN documenation states: Indicates whether SQL Server uses the column as a ROWGUID.
The T-SQL MSDN page states The timestamp syntax is deprecated. This feature will be
Joe Duffy states in the MSDN article Using concurrency for scalability that the cost
I've read the MSDN article about the layouts pass, that states: When a node

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.