Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a serialized object?

I have a lot of serialized objects saved as XML, but I would like to add 2 variables to these objects.

Here is my object:

public class MyObject{
    public Int32 MyVariables = 0;
}

This object has been serialized quite a bit, and I want keep the ability to read these "older" files into my application.

But I need to add a few more variables to make the object better, such as:

public class MyObject{
    public Int32 MyVariables = 0;
    public Dictionary<string,MyEnum> MyDict = new Dictionary<string,MyEnum>();
}

Can anyone suggest the best method to add these new variables? I actually changed MyObject and added the dictionary and I believe it is no longer being read in properly.

Thanks in advance!

Edit: I'm also not able to catch an exception anywhere to see where it fails when reading in the object, I'm doing this to do so:

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
object obj = formatter.Deserialize(File.Open(Path, FileMode.Open));

Edit 2: I believe this actually may be due to me using an Enum as part of the dictionary, I added [Serializable] above the enum and it still doesn't work - thoughts?

like image 443
Geesu Avatar asked Oct 08 '22 01:10

Geesu


1 Answers

Just add the new variables.

If the XML serializer does not find an appropriate representation for the new variables, it will initialize them with default values.

like image 184
Eric J. Avatar answered Oct 13 '22 12:10

Eric J.