Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BinaryFormatter object graph upgrade

In my object graph, I have something like

[Serializable]
public class Dog 
{
     string _name;
}

and I have all sorts of lists of Dogs and reference to Dogs.

Since Dog was only animal at the start of the software creation, I didn't have a need for any base class. Now, that need has emerged and now I would like to have

[Serializable]
public class Dog : Animal
{
    public void Bark() { }
}

[Serializable]
public class Cat : Animal
{
    public void DoTheCatStuff() { }
}

[Serializable]
public class Animal 
{
     string _name;
}

BUT: when I deserialize OLD archive, I don't have any dogs. They didn't deserialize from the archive at all.

What I would like is some advice on how to do it. If I'll need new class hierarchy and manually copy the objects from the old Dog to the new TheDog, fine, but would like to avoid it if possible.

EDIT: Some .NET gurus, WHY don't I have any Dogs?

like image 352
Daniel Mošmondor Avatar asked Nov 28 '25 06:11

Daniel Mošmondor


1 Answers

Sounds like you need an implementation of ISerializationSurrogate and some SerializationBinder magic. There is a very good discussion of this in an old MSDN Magazine column.

like image 196
Kelly Summerlin Avatar answered Nov 29 '25 20:11

Kelly Summerlin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!