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.