Backwards compatibility in .NET with BinaryFormatter

后端 未结 3 1232
广开言路
广开言路 2020-12-31 12:43

We use BinaryFormatter in a C# game, to save user game progress, game levels, etc. We are running into the problem of backwards compatibility.

The aims:

3条回答
  •  生来不讨喜
    2020-12-31 13:24

    We got the same problem in our application with storing user profile data (grid column arrangement, filter settings ...).

    In our case the problem was the AssemblyVersion.

    For this problem i create a SerializationBinder which reads the actual assembly version of the assemblies (all assemblies get a new version number on new deployment) with Assembly.GetExecutingAssembly().GetName().Version.

    In the overriden method BindToType the type info is created with the new assembly version.

    The deserialization is implemented 'by hand', that means

    • Deserialize via normal BinaryFormatter
    • get all fields which have to be deserialized (annotated with own attribute)
    • fill object with data from the deserialized object

    Works with all our data and since three or four releases.

提交回复
热议问题