C# BinaryFormatter - 使用另一个命名空间中的对象反序列化 [英] C# BinaryFormatter - Deserialize with the object in another namespace

查看:16
本文介绍了C# BinaryFormatter - 使用另一个命名空间中的对象反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我们将部分代码移至不同的项目库.

Recently, we moved a part of our code to different project library.

不幸的是,这些数据似乎已使用 BinaryFormatter 序列化到数据库中(不要问我为什么,我不知道,我讨厌这个想法).

Unfortunately, it appears that those data have been serialized into the database with a BinaryFormatter(don't ask me why, I don't know and I hate this idea).

现在我负责创建一个更新数据库的更新工具(当我们的软件检测到需要更新的数据库时,该工具会根据版本自动启动):

Now I'm responsible to create an update tool that update the database(The tool is launched automatically by our software when it detects a database that need updates, based on version):

  1. 创建新列
  2. 反序列化二进制列
  3. 将反序列化的列写入新列
  4. 删除旧的二进制列

我的问题是,当我尝试反序列化时,它告诉我:

My problem is that when I try to deserialize, it tells me that :

Unable to find assembly 'MyOldAssemblyName, Version=2.0.0.0, Culture=neutral, PublicKeyToken=a5b9cb7043cc16da'.

但是这个程序集已经不存在了.将此类放入我的更新程序"项目中没有问题,但我无法保留这个旧项目只包含此文件.

But this assembly doesn't exists anymore. I've no issue to put this class in my "updater" project, but no way that I can keep this old project only to contains this file.

有没有办法向 BinaryFormatter 指定它必须用指定的类反序列化它接收到的 Stream?

Is there a way to specify to the BinaryFormatter that it has to deserialize the Stream it receives with a specified class?

或者说程序集被重命名了,或者???

Or say that the assembly has been renamed, or ???

推荐答案

其实我觉得我自己找到了解决方案.

In fact I think I found myself the solution.

我们可以为二进制格式化程序提供一个 SerializationBinder,这将允许我们手动解析在流中找到的类.

We can give a SerializationBinder to the binary formater, which will allows us to resolve manually a class we found in the stream.

更多信息这里

这篇关于C# BinaryFormatter - 使用另一个命名空间中的对象反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆