0

I have a binary file that was serialized from a class. That class has been updated.

Now I need to deserialize the old file to the new class.

I don't have access to the old code, but i have access to the old class.

The Class was serialized using BinaryFormatter.

Is there a way to do that?

If not, maybe there a way to deserialize and save only the data that appears in both of the versions?

saluce
  • 13,035
  • 3
  • 50
  • 67
Omer Keidar
  • 82
  • 1
  • 10

1 Answers1

1

If you have the old assembly you can upgrade quite easily by:

  • deserialize to objects with old class type
  • write a mapper mapping old data fields to the fields in the new class
  • serialize the objects of the new class

In the mapper you can convert the old data, add new data etc.

  • how to create a mapper? – Omer Keidar Apr 11 '13 at 17:31
  • The Mapper should be a class which takes an object of the old class and one of the new (or creates a new object then) and copies all values to the corresponding fields from the old to the new. –  Apr 12 '13 at 06:31
  • i thought about that, but then i realized that there are some private and readonly fields. and some fiels that auto-generated at runtime and needed to be loaded to the new class as well – Omer Keidar Apr 12 '13 at 16:49
  • Well then it's getting a little bit dirty. You can use reflection to access the private fields in the old class. Fields filled at runtime should not be a problem because you actually load the old object. –  Apr 15 '13 at 08:19
  • #2: Anyway you should think about a datastructure to avoid this slow, errorprone and all in all quite hard to maintain reflection workaround in future releases. –  Apr 15 '13 at 08:23