Reputation: 279
I am new to C# and am trying to write some information to a file. I got the program running properly when I have the Car class in the same .cs file, but when I remove this class into another .cs file in the project, I get the runtime error of
"SerializationException was unhandled: The ObjectManager found an invalid number of fixups. This usually indicates a problem in the Formatter".
Below is the code with the Car class included. When I move the class to its own Car.cs file the error starts getting thrown.
namespace ConsoleApplication2
{
class Program
{
[Serializable()]
public class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public Car(string make, string model, int year)
{
Make = make;
Model = model;
Year = year;
}
}
/// <summary>
/// Deserializes list of cars and returns the list to user
/// </summary>
/// <returns>Returns deserialized car list</returns>
public List<Car> ReadList()
{
//create local list to hold data
List<Car> carList = new List<Car>();
try
{
using (Stream stream = File.Open("data.bin", FileMode.Open))
{
BinaryFormatter bin = new BinaryFormatter();
//point carList to deserialized stream
carList = (List<Car>)bin.Deserialize(stream);
}
}
catch (IOException)
{
}
return carList;
}
Upvotes: 4
Views: 6843
Reputation: 56
If you work with Asp.Net and has that problem you need to clear browser cache and cookies, because solution with clean bin folder not worked(restarting iis also did not work)
Upvotes: 0
Reputation: 152
When the data.bin was first created the class type is stored along with the data. if you change the class's namespace, then the formatter is not able to find the class that was stored.
Upvotes: 4