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:
- Handle all the required data.from Main class.
- Instantiate and pass these data to a Serializable class.
- Save to file the data from main class.
- Dispose stream and set serializable class to null.
- Deserialize.
- 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?