c# - BinaryFormatter 对象图升级

标签 c# .net class-design binaryformatter

在我的对象图中,我有类似的东西

[Serializable]
public class Dog 
{
     string _name;
}

我有各种狗的列表和对狗的引用。

由于 Dog 在软件创建之初只是动物,所以我不需要任何基类。现在,这种需求出现了,现在我想拥有

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

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

[Serializable]
public class Animal 
{
     string _name;
}

但是:当我反序列化旧存档时,我没有任何狗。他们根本没有从存档中反序列化。

我想要的是关于如何做的一些建议。如果我需要新的类层次结构并手动将对象从旧的 Dog 复制到新的 TheDog,那很好,但我想尽可能避免它。

编辑:一些 .NET 专家,为什么我没有狗?

最佳答案

听起来你需要一个 ISerializationSurrogate 的实现还有一些SerializationBinder魔法。在一个旧的 MSDN Magazine column 中对此有很好的讨论。 .

关于c# - BinaryFormatter 对象图升级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5950481/

相关文章:

c# - 用于 select 中复杂表达式的 Linq orderby

c# - 检查消息是否从azure队列中删除

c# - 如何正确命名域 namespace 内的类?

ios - 来自 Performance Wise : Class Computed Property vs Stored Property

Java:如何考虑对马尔可夫链建模?

c# - 大量数字的简单算术运算c#

c# - 这是简化继承类的有效方法吗?

C# .NET 为什么解压流时需要 Stream.Seek

c# - HttpWebRequest 问题 : System lacked sufficient buffer space or because a queue was full

使用反射时的.net运行时类型转换