Click here to Skip to main content
15,354,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
Samuel Cherinet 7-Sep-10 10:48am
   
are you using XML serialization or Binary serialization?

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.
   
Comments
Sandeep Mewara 7-Sep-10 13:46pm
   
Comment from OP:
Thanks, Original

That sounds like a very tidy way of doing things. I'll definately look into implementing it. Even so, it would save me a lot of manual updating of the code if the constructor could loop through the class' variables/properties to fetch the available counterpart in the old object. Is there any way of doing this?

Also, I realized that I missed a simple cast when doing a .CreateInstance on an array, so whipping the old object into shape is also doable. Still, being able to loop through the member variables would help a lot.

Any input would be much appreciated.
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
   
Comments
OriginalGriff 7-Sep-10 14:00pm
   
"it would save me a lot of manual updating of the code if the constructor could loop through the class' variables/properties to fetch the available counterpart in the old object"
I was going to tell you you could via reflection, but you beat me to it. :laugh:
Good luck!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900