title: Deserialization problem: Error when deserializing from a different program version
body: <p>I finally decided myself to post my problem, after a couple of hours spent searching the Internet for solutions and trying some.</p>
  
  <p><strong>[Problem Context]</strong></p>
  
  <p>I am developing an application which will be deployed in two parts:</p>
  
  <ul>
  <li>an XML Importer tool: its role is to Load/Read an xml file in order to fill some datastructures, which are afterwards serialized into a binary file.</li>
  <li>the end user application: it will Load the binary file generated by the XML Importer and do some stuff with the recovered data structures.</li>
  </ul>
  
  <p>For now, I only use the XML Importer for both purposes (meaning I first load the xml and save it to a binary file, then I reopen the XML Importer and load my binary file). </p>
  
  <p><strong>[Actual Problem]</strong></p>
  
  <p>This works just fine and I am able to recover all the data I had after XML loading, <strong>as long as I do that with the same build of my XML Importer</strong>. This is not viable, as I will need at the very least two different builds, one for the XML Importer and one for the end user application. Please note that the two versions of the XML Importer I use for my testing are <strong>exactly the same concerning the source code and thus the datastructures</strong>, the only difference lies in the build number (to force a different build I just add a space somewhere and build again).</p>
  
  <p>So what I'm trying to do is:</p>
  
  <ul>
  <li>Build a version of my XML Importer</li>
  <li>Open the XML Importer, load an XML file and save the resulting datastructures to a binary file</li>
  <li>Rebuild the XML Importer</li>
  <li>Open the XML Importer newly built, load the previously created binary file and recover my datastructures.</li>
  </ul>
  
  <p>At this time, I get an Exception:</p>
  
  <pre><code>SerializationException: Could not find type 'System.Collections.Generic.List`1[[Grid, 74b7fa2fcc11e47f8bc966e9110610a6, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]'.
  System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadType (System.IO.BinaryReader reader, TypeTag code)
  System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadTypeMetadata (System.IO.BinaryReader reader, Boolean isRuntimeObject, Boolean hasTypeInfo)
  System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectInstance (System.IO.BinaryReader reader, Boolean isRuntimeObject, Boolean hasTypeInfo, System.Int64&amp; objectId, System.Object&amp; value, System.Runtime.Serialization.SerializationInfo&amp; info)
  System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject (BinaryElement element, System.IO.BinaryReader reader, System.Int64&amp; objectId, System.Object&amp; value, System.Runtime.Serialization.SerializationInfo&amp; info)
  </code></pre>
  
  <p>For your information (don't know if useful or not), the actual type it is struggling to deserialize is a List, Grid being a custom Class (which is correctly serializable, as I am able to do it when using the same version of XML Importer).</p>
  
  <p><strong>[Potential Solution]</strong></p>
  
  <p>I do believe it comes from somewhere around the Assembly, as I read many posts and articles about this. However, I already have a custom Binder taking care of the differences of Assembly names, looking like this:</p>
  
  <pre><code>public sealed class VersionDeserializationBinder : SerializationBinder
  { 
      public override Type BindToType( string assemblyName, string typeName )
      { 
          if ( !string.IsNullOrEmpty( assemblyName ) &amp;&amp; !string.IsNullOrEmpty( typeName ) )
          { 
              Type typeToDeserialize = null; 
              assemblyName = Assembly.GetExecutingAssembly().FullName; 
              // The following line of code returns the type. 
              typeToDeserialize = Type.GetType( String.Format( "{0}, {1}", typeName, assemblyName ) ); 
  
              return typeToDeserialize; 
          } 
  
          return null; 
      }
  }
  </code></pre>
  
  <p>which I assign to the BinaryFormatter before deserializing here:</p>
  
  <pre><code>    public static SaveData Load (string filePath) 
      {
          SaveData data = null;//new SaveData ();
          Stream stream;
  
          stream = File.Open(filePath, FileMode.Open);
  
  
          BinaryFormatter bformatter = new BinaryFormatter();
          bformatter.Binder = new VersionDeserializationBinder(); 
          data = (SaveData)bformatter.Deserialize(stream);
          stream.Close();
  
          Debug.Log("Binary version loaded from " + filePath);
  
          return data; 
      }
  </code></pre>
  
  <p>Do any of you guys have an idea on how I could fix it? Would be awesome, pretty please :)</p>