- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们有一个遗留类A
,它是[Serializable]
,但不是ISerializable
。由于各种原因,必须修补此类以实现 ISerializable。
问题是我们仍然需要能够加载旧的保存文件,这些文件使用 .NET 的默认实现来序列化类 A
。换句话说,当加载旧的保存文件时,类 A
必须像旧版本一样反序列化,然后转换为类 A
的新版本,这样再次保存时,使用了我们新的 ISerializable
实现。
在不破坏向后兼容性的情况下修补类 A
的最合适的方法是什么?
最佳答案
首先,更改项目的汇编版本。
// AssemblyInfo.cs:
[assembly: AssemblyVersion("2.0.0.0")]
为游戏存档数据创建一个新类型。
// The legacy savegame data. Keep it as it is.
[Serializable]
public class Savegame
{
// ... the legacy fields
}
// The new savegame data. This implements ISerializable
[Serializable]
public class SavegameNew : Savegame, ISerializable
{
// this constructor will execute on deserialization. You will deserialize
// both the legacy and new types here.
private SavegameNew(SerializationInfo info, StreamingContext context)
{
foreach (SerializationEntry entry in info)
{
// you can iterate the serialized elements like this
// if this is a new format you will get the new elements, too
}
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
// custom serialization of the new type
}
}
现在您需要一个将旧格式映射到新格式的 Binder 类:
internal class SavegameBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
// Of course, use the assembly version of the old version here.
// You don't even need to change the assembly version, though than can lead to ambiguity
AssemblyName asmName = new AssemblyName(assemblyName);
if (asmName.Version == new Version(1, 0, 0, 0) && typeName == typeof(Savegame).FullName)
return typeof(SavegameNew);
// otherwise we do not change the mapping
return null;
}
}
你可以这样使用它:
// the saveStream can contain either the old other the new format
BinaryFormatter bf = new BinaryFormatter() { Binder = new SavegameBinder() };
SavegameNew data = (SavegameNew)bf.Deserialize(saveStream);
使用此解决方案,您无需将 Savegame
映射到 SavegameNew
。如果不更改程序集版本,则甚至不需要 binder 类。
如果更改程序集版本,SavegameBinder
应返回新程序集的 Savegame
类型。遗留的 Savegame
应该实现 IObjectReference
接口(interface),所以一旦它被反序列化,它就可以返回一个 SavegameNew
实例。
// The legacy savegame data
[Serializable]
public class Savegame: IObjectReference
{
// ... the legacy fields
public object GetRealObject(StreamingContext context)
{
return new SavegameNew(...);
}
}
关于c# - 如何用新的 ISerializable 类替换遗留类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33544378/
我目前正在努力保存更大的对象结构,其中包含我正在处理的应用程序中的“项目”所需的所有数据。数据是诸如图片、流文档以及更基本的数据类型之类的东西。 现在,我目前的方法是在我需要保存的对象中包含的所有类上
我有一个类,其中包含我正在序列化的一些数据: [Serializable] public class SomeData { public int NumericField; publi
我必须使用一个旧应用程序,该应用程序使用 binaryFormatter 将应用程序数据序列化为文件流(例如在名为“data.oldformat”的文件中) 没有任何优化,主类已用属性标记 publi
考虑以下类的 POD 类型: public class Price { public decimal OfferPrice { get; set; } } 此类的对象是从服务器检索的,所以让我们用可序
我打算使用序列化来做克隆。我必须让我的类 ISerializable。但是它的父类(super class)和所有引用的变量类呢?我需要让它们全部 ISerializable 吗? 如果我使用 ISe
ISerializable 接口(interface)只提供了一种序列化对象的方法。反序列化进程由构造函数管理。 问题是,构造函数不能返回实例,因为构造函数创建了一个新实例。 在我的实现中,有几个属性
我们的任务非常简单,我们有一个对象图,其中每个对象 (IDItem) 都有一个唯一的 ID。对象图存在两次,在客户端和服务器机器上。 现在我们将一些可序列化的命令传递给服务器。该命令具有一些 IDIt
我正在编写自己的 IFormatter 实现,但我想不出一种方法来解决两种都实现 ISerializable 的类型之间的循环引用。 这是通常的模式: [Serializable] class Foo
我们有一个遗留类A,它是[Serializable],但不是ISerializable。由于各种原因,必须修补此类以实现 ISerializable。 问题是我们仍然需要能够加载旧的保存文件,这些文件
我想为包含相似类型子项列表的 C# 类实现 ISerializable。考虑以下示例: using System; using System.Collections.Generic; using Sy
我很难理解 ISerializable 接口(interface)的必要性......我想我在这个主题中遗漏了一些非常重要的东西,所以如果有人能帮助我,我将不胜感激。 这很好用- [Serializa
我有扩展方法 public static T DeepClone(this T source) where T : ISerializable { .. } 当我添加“where T : I
我正在尝试使用 Iserializable 在 Android 中传递对象,但它返回“无法从 native 句柄激活类型的实例”异常。 下面是我的代码。 [FBUserParcel.cs] using
我正在编写自定义序列化程序,我刚刚完成了用于处理 ISerializable.GetObjectData 的部分的实现。但是当我去反序列化信息并将其重新应用于图形时,我没有看到 Set-ObjectD
我正在阅读 msdn 上的自定义序列化文章:http://msdn.microsoft.com/en-us/library/ty01x675%28VS.80%29.aspx 里面提到了实现自定义序列化
我必须在派生类中实现 ISerializable(以执行一些自定义序列化/反序列化),但父类被标记为 [Serializable]。序列化“有效”(我可以序列化和反序列化而不会出现运行时错误)但看起来
我有一组用户定义的类型,它们已经实现了 ISerializable 接口(interface),现在我想将它们托管在服务器端应用程序中,并通过使用 DataContract 属性标记它们来向客户端公开
我有一个看起来像这样的包装列表: [JsonObject(MemberSerialization.Fields)] public class OrderManager : IEnumerable, I
我对继承类中 ISerializable 的正确实现有疑问。 我有两个类,AbstractBaseClass 及其实现 BaseClass。在实现派生自 BaseClass 的 FinalClass
我有一个类通过 XLINQ 实现它自己的(反)序列化,我会喜欢让我类(class)的客户尝试对我的类(class)进行 XMLSerialze而是调用了我的 XLINQ 方法。 这可能吗? 最佳答案
我是一名优秀的程序员,十分优秀!