4

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

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Interesting, I deleted data.bin and reran the program and it worked perfectly, thanks for the help
Don't suppose there's a way to override the deserializer's behaviour to tell it to interpret a class from one namespace as the same class in a different namespace?
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.