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