.NET二进制格式反序列化一个对象,其定义已经发生了很大变化 [英] .net binary formatter deserialize an object whose definition has changed a lot
问题描述
我想反序列化是使用的是旧版本的序列化的文件。
I am trying to deserialize a file which is serialized using an older version.
在新版本中,我们做了以下事情
In the new version, We have done the following things
- 更改命名空间
- 更改类成员访问级别,从私人到公共
- 添加一个新的跨父类。 A类来源于基地。现在,一类是从乙衍生,和B从基地而得。在B,没有新的部件引入。
- 在基类增加了新的成员。
我知道用SerializationBinder就可以解决的问题1.对于新增加的类成员,我已经标有[非序列化。但我仍然得到反序列化的错误。
I know using SerializationBinder can solve issue 1. For the new added class member, I have marked with [NonSerialized]. But I still get deserialization error.
任何指针?
推荐答案
我讨厌这样说,但我在这里的第一个指针是不这样做 - 的BinaryFormatter
很脆 - 而你的可以的经常砰键盘上你的头6个小时才能过去每个连续的痛点,这真的是不值得的。有很多很多的原因,我总是说不使用的BinaryFormatter
- 你已经伤心地发现其中4个。坦率地说,我的建议是:
I hate to say it, but my first pointer here would be "don't do that" - BinaryFormatter
is very brittle - and while you can often bang your head on the keyboard for 6 hours to get past each successive pain point, it really isn't worth it. There are many many reasons I always say "don't use BinaryFormatter
" - you've sadly found 4 of them. Frankly, my recommendation would be:
- 放回到原来的班级,并使用它只是作为一个DTO反序列化的旧数据
- 在开始移动到一个模型,你有一个单独的DTO模型和实体模型 - 以及它们之间的映射,使您的序列是完全独立应用程序的逻辑
- 在探讨不同序列化器;几乎所有的替代串是preferable到
的BinaryFormatter
(也许除了NetDataContractSerializer
,肚里并重复所有同样的错误;的DataContractSerializer
是好的杂交,虽然)
- put back the original class, and use it just as a DTO to deserialize the old data
- start moving to a model where you have a separate DTO model and entity model - and map between them so that your serialization is entirely independent of your application logic
- investigate different serializers; just about any alternative serializer would be preferable to
BinaryFormatter
(except perhapsNetDataContractSerializer
, which goes and repeats all the same mistakes;DataContractSerializer
is ok-ish, though)
对不起,是不是更积极的......
Sorry that isn't more positive...
这篇关于.NET二进制格式反序列化一个对象,其定义已经发生了很大变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!