Serialization exception using C #

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;
        }
     }
    /// <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;
     }
+4
source share
1 answer

data.bin , . , .

+2

All Articles