title: How to deserialize an class into different class (in new namespace) using BinaryFormatter (C#)
body: <p>I have a class <code>User</code> belong to <code>Authentication</code> namespace, as below:</p>
  
  <pre><code>namespace Authentication {
      public class User 
      {
         public string Name {get;set;}    
      }
  }
  </code></pre>
  
  <p>I serialized an object of class User  using BinaryFormatter into byte[ ] and store it in database.</p>
  
  <pre><code>User usr = new User();
  usr.Name = "myself";
  var usrValue = convertObjectToByteArr(usr);
  //then store usrValue into db.
  </code></pre>
  
  <p>After some time, class User has new property and moved to new namespace: <code>Authentication.Organization</code></p>
  
  <pre><code>namespace Authentication.Organization{
      public class User 
      {
          public string Name {get;set;}
  
          public int Age {get;set;}
      }
  }
  </code></pre>
  
  <p><strong>My question: How do I de-serialize the object of <code>previous</code> User into <code>current</code> User ?</strong></p>
  
  <p>I'll hit exception: <strong><code>{"Unable to load type Authentication.User required for deserialization."}</code></strong> when I try to deserialize it:</p>
  
  <pre><code>byte[] prevUsrValue= (byte[])helper.getPreviousUserValue();//read from database
  
  User previousUser = convertByteArrToObject(prevUsrValue) as User;
  </code></pre>
  
  <hr>
  
  <p>FYI, I am using these method to serialized and de-serialize:</p>
  
  <pre><code>//serialization
  public static byte[] convertObjectToByteArr(object o)
  {
      if (o != null)
      {
          System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
          System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
          binaryFormatter.Serialize(memoryStream, o);
          return memoryStream.ToArray();
      }
      return null;
  }
  
  //de-serialization
  public static object convertByteArrToObject(byte[] b)
  {
      if (b != null &amp;&amp; b.Length &gt; 0)
      {
          System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(b);
          System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
          memoryStream.Position = 0;
          return binaryFormatter.Deserialize(memoryStream);
  
      }
      return null;
  }
  </code></pre>