.net - 如何反序列化有些不兼容的 BinaryFormatter 数据?

标签 .net serialization

我有一个在 .NET 1.1 中开发的继承代码库。当我将其迁移到2.0时,我重命名了一个程序集。现在,我发现有必要读取包含由旧程序集和 .NET 1.1 序列化的对象的旧文件。 某些字段已成功反序列化,但大多数都被忽略。如果我启用 FormatterAssemblyStyle.Full ,我会收到如下错误:

类“MyNamespace.T_VERSION”中的成员“T_VERSION+s_sys_ver”不存在于序列化流中,并且未使用 System.Runtime.Serialization.OptionalFieldAttribute 进行标记。

对二进制文件的检查表明,T_VERSION+s_sys_ver 以某种形式存在,并且字段类型没有改变。

除了手动逆向工程旧格式并编写我自己的手动反序列化例程之外,是否有任何方法可以反序列化旧格式文件?

最佳答案

检查SerializationBinder

During serialization, a formatter transmits the information required to create an instance of an object of the correct type and version. This information generally includes the full type name and assembly name of the object. The assembly name includes the name, version, and strong name (see Strong-Named Assemblies) hash of the assembly. By default, deserialization uses this information to create an instance of an identical object (with the exception of any assembly loading restricted by the security policy). Some users need to control which class to load, either because the class has moved between assemblies or a different version of the class is required on the server and client.

关于.net - 如何反序列化有些不兼容的 BinaryFormatter 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/736615/

相关文章:

c# - 使用 Visual C# (Dot Net Framework 4.0) 在 "Socket Programming"中需要帮助?

c# - 无法在网络方法中读取内存

c# - RegistryKey.GetSubKeyNames 中元素的顺序

java自定义类不可序列化

java - 在操作调用中生成 Struts 2 JSON 输出

c# - 如何使用 XmlWriter.Create(...?

c# - 当 EventLogEntry.Index 达到 MaxInt 时会发生什么?

java - 反序列化 JSON 时,RuntimeTypeAdapterFactory 'typeFieldName' 应该适合什么字符串?

c# - 我可以让 XmlSerializer 在反序列化时忽略命名空间吗?

c# - 序列化/反序列化为字符串 C#