简体   繁体   中英

Updating properties of binary Serialized class c# .NET effectively corrupts files

I have created a Car class that I am using to store preset serialized versions of Cars with all their various properties and what have you.

I have realised as my program progresses that if I add or remove a property of the Car, every previously serialized file is now unreadable. This as you can imagine, is a problem.

How can I update my class without invalidating all previous files?

-- Clickety

Update: I've added a code sample of what I'm doing The issue arises if I add/remove a property and try to deserialize a file.

using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
using System;


namespace MyCars
{

    [Serializable]
    public class Car
    {
        public string Name { get; set; }
        public double TopSpeed { get; set; }

        public Car(string name, double topspeed)
        {
            Name = name;
            TopSpeed = topspeed;
        }
    }


    public static class Serializer
    {
        public static bool LoadCar(string filePath, out Car car)
        {
            Stream TestFileStream = File.OpenRead(filePath);
            BinaryFormatter serializer = new BinaryFormatter();

            try
            {
                car = (Car)serializer.Deserialize(TestFileStream);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Could not deserialize");
                TestFileStream.Close();
                car = null;
                return false;
            }

            return true;
        }

        public static bool SaveCar(string filePath, Car car)
        {
            Stream TestFileStream = File.Create(filePath);
            BinaryFormatter serializer = new BinaryFormatter();
            serializer.Serialize(TestFileStream, car);
            TestFileStream.Close();

            return true;
        }
    }

}

Binary serialization, as implemented by System.Runtime.Serialization.Formatters.Binary , is a schema-less binary format. What that means is that if the serializer sees an int field, it writes the 4 bytes in the file, same with all the other supported types. It's very fast, but not flexible.

This is different from a serializer that also writes the schema, which would write the 4 bytes, as well as the field name, so later the deserializer can mix and match. As you can imagine, it's slower but much more flexible.

As long as you're sticking with the former, simply don't change the schema. There isn't much else you can do with it.

This is why everyone is going to (and already has) recommend using a schema serializer, like JSON, BSON (binary json), Google's proto-buf, XML, an Sql or Nosql database, anything else really, especially during development when class schemas change often.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM