I am new to C # and trying to write some information to a file. I got the program correctly when I have the Car class in the same .cs file, but when I delete this class in another .cs file in the project, I get a runtime error
"The error SerializationException was unhandled: the ObjectManager detected an invalid number of patches, which usually indicates a problem in the Formatter."
Below is the code with the Car class enabled. When I move the class to my own Car.cs file, the error starts throwing.
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;
}
}
public List<Car> ReadList()
{
List<Car> carList = new List<Car>();
try
{
using (Stream stream = File.Open("data.bin", FileMode.Open))
{
BinaryFormatter bin = new BinaryFormatter();
carList = (List<Car>)bin.Deserialize(stream);
}
}
catch (IOException)
{
}
return carList;
}
source
share