类更新后进行反序列化 [英] Deserialization after class update

查看:72
本文介绍了类更新后进行反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我正在使用序列化来保存类的对象,该类用于保存一些复杂的自定义数据结构.我的想法是使用序列化作为在应用程序中加载和保存数据的方式.这种方法对我有很多好处.但是,在版本处理方面存在一个问题:

使用程序一段时间后,我希望对类进行更改,这会使反序列化过程变得有些复杂.在向类中添加其他数据结构后,将加载旧对象,但不会初始化新数据结构.任何访问它们的尝试都会导致Null引用异常:对象引用未设置为对象的实例".

我看到了两种解决方案(尽管您可能会看到更多):

1:反序列化旧对象后,强制新成员变量初始化.我尝试在我的缺失"变量上同时运行两种方法.CreateInstance和.Initialize,但均无济于事. CreateInstance已完成,但是.Initialize只会导致另一个Null引用异常. (调试显示,即使反序列化后,新变量仍存在于对象中,但列为Nothing.)是否可以强制公共成员变量进行初始化?

2:创建新类版本的新实例,然后将数据从反序列化的对象移动到新的实例.然后,新成员应具有所有成员的初始化和功能.此解决方案要求我遍历新对象的所有成员变量,并从旧对象中复制它们的内容(如果它们存在的话).我的问题是遍历新对象的成员变量和属性:如何以编程方式获取对象(或其类)的所有变量和属性的列表?

也欢迎任何其他解决方案.

解决方案

这是我尝试避免使用序列化的原因之一.
就个人而言,我将在新类中具有一个构造函数/赋值运算符/方法,该方法接受旧类的实例并复制相关字段.这样,只有更新的类才知道更改.

似乎我偶然发现了解决方案,它可以为类再次更改做好准备.
.GetType.GetFields返回一个公共变量数组(在System.Reflection命名空间中为FieldInfo类型)
.GetType.GetProperties返回一个公共属性数组(在同一名称空间中为PropertyInfo类型)

我希望这可以对其他人有所帮助.

干杯,
Fossie


Hello all

I''m using serialization to save objects of a class which I use to hold some complex custom data structures. My idea was to use serialization as a way to load and save data in my application. This approach has numerous benefits for me. However, there is one problem when it comes to version handling:

After using my program for a while I expect to make changes to my class, which complicates the deserialization procedure somewhat. After adding additional data structures to the class, the old objects will load, but the new data structures will not be initialized. Any attempt to access them results in a Null Reference Exception: "Object reference not set to an instance of an object".

I see two solutions to this (although you might see more):

1: Force the new member variable to initialize after deserializing the old object. I tried running both methods .CreateInstance and .Initialize on my "missing" variable, but to no avail. CreateInstance is completed, but .Initialize results in just another Null Reference Exception. (Debugging shows the new variable exists in the object even after deserializing, but listed as Nothing.) Is there a way to force a public member variable to initialize?

2: Create a new instance of the new class version, and move the data from the deserialized object to the new one. The new one should then have all members initialized and functioning. This solution requires me to loop over all member variables of the new object, and copy their contents from the old object if they exist there. My problem here is looping over the new object''s member variables and properties: How can I programmatically fetch a list of all variables and properties of an object (or it''s class)?

Any other solutions are also most welcome.

解决方案

This is one of the reasons why I try to avoid using serialization.
Personally, I would have a constructor / assignment operator / method in the new class which accepts an instance of the old class and copies the relevant fields over. That way only the updated class is aware of the changes. It can also be ready for when the class changes again.


It seems I stumbled upon the solution;
.GetType.GetFields returns an array of public variables (of type FieldInfo, in the System.Reflection namespace)
.GetType.GetProperties returns an array of public properties (of type PropertyInfo in the same namespace)

I hope this can be of help to others.

Cheers,
Fossie


这篇关于类更新后进行反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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