开发者

BinaryFormatter object graph upgrade

开发者 https://www.devze.com 2023-03-05 07:18 出处:网络
In my object graph, I have something like [Serializable] 开发者_如何学Gopublic class Dog { string _name;

In my object graph, I have something like

[Serializable]
开发者_如何学Gopublic 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?


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.


This is an interesting one; my gut instinct is to do this:

I would write an upgrade program that has the old class format that writes it out to the new format, or an intermediate format that you can then comprehend and convert to your new class hierarchy.

Perhaps by deserialising the binary format of the old archive, XML serialize it and then maybe use LinqToXml to create objects in your new hierarchy.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号