C#的BinaryFormatter - 反序列与另一个命名空间中的对象序列、对象、空间、BinaryFormatter

2023-09-03 06:47:23 作者:阿猫阿狗^ō^

最近,我们提出我们的code到不同的项目库的一部分。

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):

创建新列 反序列化的二进制列 写反序列化列到新列 删除旧的二进制列

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

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的,它有反序列化它收到指定类的流?

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.

更多信息这里