title: Deserialize Revit API
body: <p>this is my first time to get my feet wet with serialization...in fact i am developing Autodesk Revit through C#.</p>
  
  <p><strong>Objective:</strong></p>
  
  <p>I need to record data to a new file on HDD, so that this file can be opened from another computer through Revit.</p>
  
  <p><strong>Procedure:</strong> </p>
  
  <ol>
  <li>Handle all the required data.from Main class.</li>
  <li>Instantiate and pass these data to a Serializable class.</li>
  <li>Save to file the data from main class.</li>
  <li>Dispose stream and set serializable class to null.</li>
  <li>Deserialize.</li>
  <li>Do stuff on revit on the basis of acquired data.</li>
  </ol>
  
  <p><strong>Problem</strong>
  - the program works perfectly  with no error and every thing is OK.
  - press the button again to rerun the program it fails at deserialization with this error code</p>
  
  <blockquote>
    <p>[A]Cons_Coor.ThrDviewData cannot be cast to [B]Cons_Coor.ThrDviewData. Type A originates from 'Cons_Coor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location 'C:\Users\mostafa\AppData\Local\Temp\RevitAddins\Cons_Coor-Executing-20140820_224454_4113\Cons_Coor.dll'. Type B originates from 'Cons_Coor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location 'C:\Users\mostafa\AppData\Local\Temp\RevitAddins\Cons_Coor-Executing-20140820_230011_0316\Cons_Coor.dll'.A first chance exception of type 'System.NullReferenceException' occurred in Cons_Coor.dll</p>
  </blockquote>
  
  <p><strong>Main Class:</strong></p>
  
  <pre><code>///main class
  .....
  .....
  
  ThrDviewData v3ddata = new ThrDviewData(); ///instantiate a serializable class
  
  ///collect all required data
  
  string filename = UT_helper.conpaths(UT_constants.paths.Desktop) + "\\comment2" +      DateTime.Today.ToShortTimeString().Replace(":", "") + ".a4h";
  
          using (Stream stream = File.Open(filename, FileMode.Create))
          {
  
              BinaryFormatter bformatter = new BinaryFormatter();
  
              Debug.WriteLine("Writting Data\r\n");
              bformatter.Serialize(stream, v3ddata);
  
              stream.Close();
  
          }
  
          v3ddata = null;
  
          using (Stream stream = File.Open(filename, FileMode.Open))
          {
  
              BinaryFormatter bformatter = new BinaryFormatter();
  
              Debug.WriteLine("Reading data from file");
              try
              {
                  v3ddata = (ThrDviewData)bformatter.Deserialize(stream);
  
              }
              catch (Exception ex)
              {
                  Debug.Write(ex.Message);
                  //  File.Delete(filename);
              }
  
              stream.Close();
          }
  ....
  ....
  ///do some stuff with the acquired data
  </code></pre>
  
  <p><strong>Serializable Class</strong></p>
  
  <pre><code>  public string myvariables;
  
  
      public ThrDviewData()
      {
  
          myvariables = null;
  
  
      }
  
  
      public ThrDviewData(SerializationInfo info, StreamingContext ctxt)
      {
              myvariables= (String)info.GetValue("name", typeof(string));
  
      }
  
  
      public void GetObjectData(SerializationInfo info, StreamingContext context)
      {
          info.AddValue("name", myvariables);
  }
  
  
      // Public implementation of Dispose pattern callable by consumers. 
      public void Dispose()
      {            
          GC.SuppressFinalize(this);
      }
  
  
  }
  </code></pre>
  
  <p>so any hints?</p>