Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Serialization and versionning

Serialization and versionning

Scheduled Pinned Locked Moved C#
questiondata-structuresjsonperformanceannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stephane David
    wrote on last edited by
    #1

    I'm developping an application where I need to serialize objects. But the properties of these objects may change during the developpment as I could add new options. For instance, I may have this class Car { // version1 string ModelName; int Speed_Mph; } And then I want to serialize an array of Car into a binary file (foo.sav) Later, I add the color : class Car { // version1 string ModelName; int Speed_Mph; // version 1.1 string Color; } Now, if I try to used the deserialization function with foo.sav, it won't work, because foo.sav didn't contain the Color properties for Car. What is the easiest / msot efficient way to solve it? I have several ideas in mind, but I don't know the better ones. The most efficient one seems to be to write my own ISerializable interface, and start by reading the version. It works, but I'm afraid it's a lot of work, as I need to assign the keys one by one, needn't I?

    H 1 Reply Last reply
    0
    • S Stephane David

      I'm developping an application where I need to serialize objects. But the properties of these objects may change during the developpment as I could add new options. For instance, I may have this class Car { // version1 string ModelName; int Speed_Mph; } And then I want to serialize an array of Car into a binary file (foo.sav) Later, I add the color : class Car { // version1 string ModelName; int Speed_Mph; // version 1.1 string Color; } Now, if I try to used the deserialization function with foo.sav, it won't work, because foo.sav didn't contain the Color properties for Car. What is the easiest / msot efficient way to solve it? I have several ideas in mind, but I don't know the better ones. The most efficient one seems to be to write my own ISerializable interface, and start by reading the version. It works, but I'm afraid it's a lot of work, as I need to assign the keys one by one, needn't I?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      One way would be to implement the ISerializable interface and take these things into account yourself. Another way - especially if your assemblies are versioned (or better yet, strongly named) - is to extend the SerializationBinder and override BindToType so that when an older Type (since a Type name includes not only the fully-qualified class name but also the assembly information) you bind it to your new Type. You'd still have to take into account any changes, but this is a common and pretty efficient way of solving the problem of versioning. See the documentation for the SerializationBinder method in the .NET Framework SDK for more information and an example.

      Microsoft MVP, Visual C# My Articles

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups