重命名字段,然后反序列化在C# [英] Renaming fields then deserializing in C#

查看:312
本文介绍了重命名字段,然后反序列化在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有存储在其中已经系列化与.NET的BinaryFormatter的类的实例数据。我现在想重命名类的领域之一,但依然能够反序列化的旧数据。

I have data stored in an instance of a class which has been serialized with the .net BinaryFormatter. I now want to rename one of the fields in the class, but still be able to deserialize the old data.

一种选择是实施了ISerializable和反序列化的所有领域之类的手动操作。但是,这似乎是一个大量的工作,尤其是当我班有很多领域,我已经改名只是一个字段。

One option is to implement ISerializable and deserialize all the the fields of the class manually. But this seems like a lot of work, especially if my class has lots of fields and I've only renamed a single field.

有没有更好的办法?


克雷格建议保留旧类的副本进行反序列化和复制值到新类。我已经看到了这个建议太别处 - 这是否有什么优势实现了ISerializable?至于我可以看到,复制类的给我留下了2几乎相同类的副本再加上我还是要所有的值从旧的类复制到新的等级 - 这似乎是相同的工作数量与一个执行了ISerializable几乎重复扔进混合类。

Craig suggests keeping a copy of the old class for deserialization, and copying values to the new class. I've seen this suggested elsewhere too - what advantage does this have over implementing ISerializable? As far as I can see, copying the class leaves me with 2 almost identical copies of the class plus I still have to copy all the values from the old class to the new class - which seems the same amount of work as implementing ISerializable with an almost duplicate class thrown into mix.


两个答案都提到粘合剂。我已经成功地使用了SerializationBinder反序列化其序列化为类Foo类吧,但那是因为类的名称已更改。是否SerializationBinder也有助于当你改名场 - 说,当INT m_Left已更名为int m_Right

Two answers have mentioned Binders. I've successfully used a SerializationBinder to deserialize a class Bar which was Serialized as class Foo, but that's because the name of the class changed. Does SerializationBinder also help when you've renamed a field - say when int m_Left has been renamed to int m_Right?

推荐答案

您可以?尝试有老CALSS流连补液的唯一目的,然后刚过,你需要在新的类中的字段复制。还是那种痛苦的,但应该工作。

You could try having the old calss hang around for the sole purpose of rehydration and then just copy over the fields you need to the new class. Still kind of painful but should work.

这篇关于重命名字段,然后反序列化在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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