Deserialization Casting Exception After Project Rebuild

  • Thread starter Thread starter Dave L
  • Start date Start date
D

Dave L

I am using a stream and binary formatter to store data onto the disk.

BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, header);
formatter.Serialize(stream, data);

Later I read the data back:

BinaryFormatter formatter = new BinaryFormatter();
header = (Header)formatter.Deserialize(stream);
playbackData = (ArrayList)formatter.Deserialize(stream);

I then obtain an instance from my array:

Message msg = (Message)playbackData[0];

This all works, until I rebuild my application and try to read a previously
stored file on the disk drive. As soon as I rebuild, opening any file
created with the previous build generates a: "System.InvalidCastException -
Specified cast is not valid" on the above line.

I can see all the data from within the ArrayList and its correct. The
GetType() returns the correct type. But casting causes an exception. Why?

Of note: The data type I'm storing in the ArrayList is managed C++ and
within a DLL. The DLL is in the GAC. All the above code is C#. Should make a
difference, right?

Thanks in advance for the help.
 
Back
Top