title: Deserialization constructor not called
body: <p>I am attempting to serialize/deserialize an object that contains a <code>Dictionary&lt;Tuid,Section&gt;</code>. These are both custom types.</p>
  
  <p>In my code I have a type of <strong>Template</strong> which contains the <code>Dictionary&lt;Tuid,Section&gt;</code>. It is the <strong>Template</strong> class that I am attempting to serialize/deserialze.</p>
  
  <p>To resolve the problem that this collection is a Dictionary I have implemented the <code>ISerializable</code> interface on my Template class....</p>
  
  <pre><code>[Serializable]
  public class Template : ISerializable
  {
      protected Template(SerializationInfo info, StreamingContext context)
      {
          // Deserialize the sections
          List&lt;Tuid&gt; tuids = (List&lt;Tuid&gt;)info.GetValue("Sections_Keys", typeof(List&lt;Tuid&gt;));
          List&lt;Section&gt; sections = (List&lt;Section&gt;)info.GetValue("Sections_Values", typeof(List&lt;Section&gt;));
          this._sections = new Dictionary&lt;Tuid, Section&gt;();
  
          for (int i = 0; i &lt; tuids.Count; i++)
          {
              _sections.Add(tuids[i], sections[i]);
          }           
      }
  
      public void GetObjectData(SerializationInfo info, StreamingContext context)
      {
          List&lt;Tuid&gt; tuids = new List&lt;Tuid&gt;();
          List&lt;Section&gt; sections = new List&lt;Section&gt;();
  
          foreach (KeyValuePair&lt;Tuid, Section&gt; kvp in _sections)
          {
              tuids.Add(kvp.Key);
              sections.Add(kvp.Value);
          }
  
          info.AddValue("Sections_Keys", tuids, typeof(List&lt;Tuid&gt;));
          info.AddValue("Sections_Values", sections, typeof(List&lt;Section&gt;));
     }
  </code></pre>
  
  <p>The strategy here is to "unpack" the dictionary into two seperate lists and store them individually in the serialized stream. Then they are re-created afterwards.</p>
  
  <p>My <strong>Section</strong> class also implments <code>ISerializable</code>...</p>
  
  <pre><code>[Serializable]
  public class Section : BaseObject
  {
  
      protected Section(SerializationInfo info, StreamingContext context):base(.....)
      {
          // Code
      }
  
      public void GetObjectData(SerializationInfo info, StreamingContext context)
      {
         // code
      }
  }
  </code></pre>
  
  <p>The problem is that when I serialize <code>GetObjectData()</code> is called on both my Template <em>and</em> my Section which makes me believe that the data is Serializable and that its getting serialized.</p>
  
  <p>When I deserialize, <em>only</em> the deserialize constructor on Template is called. The deserialize constructor for Section is never called. The result of this is that the call to <code>info.GetValue("Section_Values"....)</code> <em>does</em> return a List but it has one item in it and that item is null.</p>
  
  <p>Why does my constructor to deserialize a Section never get called? Could it be that some of the data inside the section is not serializable? If so, how to find out what exactly it cannot serialize?</p>
  
  <p>Update: One thing I have just spotted is that the BaseObject for section is marked with <code>[Serializable]</code> but does not implement <code>ISerializable</code>.</p>
  
  <p>Additionally, Im wondering how fussy the Deserialize code is - will it target a constructor that also constructs a base class?</p>
  
  <p><strong>Update..</strong></p>
  
  <p>Ok, Ive tracked down the problem to the Serialization of the Section. The code looks something like this...</p>
  
  <pre><code>protected Section(SerializationInfo info, StreamingContext context):base(.....)
  {
      // Code
  }
  
  public void GetObjectData(SerializationInfo info, StreamingContext context)
  {
  
      //info.AddValue("CustomObject", ClientInfo, typeof(CustomObject));
      //info.AddValue("Description", Description, typeof(string));
  }
  </code></pre>
  
  <p>With <em>both</em> of the lines commented out, nothing is serialized and the deserialization constructor is called on <code>Section</code>. If I add in the string value everything is still fine. However, yes - you guessed it - if I add the <code>CustomObject</code> into the serialization stream then the deserialization constructor is not called.</p>
  
  <p>Note that...</p>
  
  <ul>
  <li>My deserialization constructor for <code>Section</code> is a blank method - I dont attempt to do anything with the deserialized data.</li>
  <li>The base constructor for Section has been stubbed out to pass in new valid objects and I have confirmed that this runs fine.</li>
  <li>No exceptions are thrown to tell me that the <code>CustomObject</code> cannot be serialized.</li>
  <li>The <code>CustomObject</code> is serializable and its <code>GetObjectData()</code> method runs fine and its constructed fine on deserialization.</li>
  </ul>
  
  <p>It seems strange that purely be adding this serializable object to the stream that the framework then just fails to the deserializer constructor of <code>Section</code>!! </p>
  
  <p>Why could this possibly be happening?</p>