Deserialize Revit API

490 Views Asked by At

this is my first time to get my feet wet with serialization...in fact i am developing Autodesk Revit through C#.

Objective:

I need to record data to a new file on HDD, so that this file can be opened from another computer through Revit.

Procedure:

  1. Handle all the required data.from Main class.
  2. Instantiate and pass these data to a Serializable class.
  3. Save to file the data from main class.
  4. Dispose stream and set serializable class to null.
  5. Deserialize.
  6. Do stuff on revit on the basis of acquired data.

Problem - 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

[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

Main Class:

///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

Serializable Class

  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);
    }


}

so any hints?

1

There are 1 best solutions below

0
On

The Binary Serializer you're using is very tightly tied to the class you're exporting. When you're using the Revit Addin manager to load your addin, it's making a dynamic copy of your assembly (so that you can come back and load it again while you're in the same session). When it does that, you wind up with duplicate types that have the same name (ThrDviewData). When you try to load a previously serialized binary that was from a different copy, it is still trying to map to the original type (not the new copy of the type).

Your choices are: 1. Don't use the Addin manager, just statically use your addin. 2. Use something other than the Binary serializer that is not so tightly coupled to the types (like an XML or JSON serializer - as you tried).

That's what happened...