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

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

问题描述

最近,我们提出我们的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):

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

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.

更多信息<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.binder.aspx"相对=nofollow>这里

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

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