如何重构.NET中序列化的类?
我有一个由 BinaryFormatter,比如这个例子:
// Version 3.0
[Serializable]
public class Person
{
public string FullName;
[OptionalField(VersionAdded=2)]
public string NickName;
[OptionalField(VersionAdded=2)]
public DateTime BirthDate;
[OptionalField(VersionAdded=3)]
public int Weight;
}
稍后,我想通过以下一项或多项来重构这个类
- 更改名称
- 更改其命名空间
- 移动到另一个程序集
据我所知,只有当具有完全相同的名称、命名空间和程序集名称的类可用时,才能反序列化二进制文件。
我该如何解决这个问题?
是否可以将反序列化映射到不同的类名、命名空间和程序集而不破坏 版本容忍序列化?
I have a C# class that is serialized to disk by the BinaryFormatter, such as this example:
// Version 3.0
[Serializable]
public class Person
{
public string FullName;
[OptionalField(VersionAdded=2)]
public string NickName;
[OptionalField(VersionAdded=2)]
public DateTime BirthDate;
[OptionalField(VersionAdded=3)]
public int Weight;
}
Later, I want to refactor this class by one or more of the following
- Change its name
- Change its namespace
- Move to another assembly
As far as I can tell, the binary file can only be de-serialized if a class with the exact same name, namespace and assembly name is available.
How do I work around this?
Is it possible to map the de-serialization to a different class name, namespace and assembly without breaking Version Tolerant Serialization?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过一些研究,我意识到 BinaryFormatter 确实支持我正在寻找的一切。
BinaryFormatter 可以使用代理来
还可以使用SerializationBinder 将反序列化从类型A 映射到类型B(不同的类名、命名空间和/或程序集名称)。
据我所知,这使得在进行仅版本控制不支持的重大更改时可以重构序列化的类并保持向后兼容性。
参考: http://www.diranieh.com/NETSerialization/BinarySerialization.htm
编辑:顺便说一句,重构字段(名称或类型)仍然很痛苦,如 在 C# 中重命名字段然后反序列化。我目前正在研究 protobuf-net 以在将来更好地解决这个问题。
After some research I realized that the BinaryFormatter does support everything I was looking for.
A BinaryFormatter can use surrogates to
One can also map deserialization from type A to type B (different class name, namespace and/or assembly name) by using SerializationBinder.
As far as I can tell, this makes it possible to refactor classes that are serialized and to maintain backwards compatibility when making breaking changes that is not supported by versioning alone.
Reference: http://www.diranieh.com/NETSerialization/BinarySerialization.htm
Edit: On a side note, refactoring fields (name or type) is still a pain, as discussed in Renaming fields then deserializing in C#. I am currently looking into protobuf-net to better solve this in the future.
您可以实现
ISerialized
接口和覆盖GetObjectData
提供您自己的反序列化。我没有尝试过,但您应该能够“手动”反序列化您的旧对象。You can implement the
ISerializable
interface and overrideGetObjectData
to provide your own deserialization. I have not tried, but you should be able to deserialize your old object "manually".