I have this class
[Serializable]
public class myClass() : ISerializable
{
public int a;
public int b;
public int c;
public void GetObjectData(SerializationInfo info,
StreamingContext context)
{
// Some code
}
public myClass(SerializationInfo info,
StreamingContext context)
{
// Some code
}
}
I have hundreds of these object in my database. I am now ready to publish a new version of my app where the class has morphed to
[Serializable]
public class myClass() : ISerializable
{
public int a;
public string b;
public int c;
public bool d;
public void GetObjectData(SerializationInfo info,
StreamingContext context)
{
// Some code
}
public myClass(SerializationInfo info,
StreamingContext context)
{
// Some code
}
}
How would one deserialize an object serialized based on the first version with the de-serialization constructor of the second.
Are there also strategies for future version proofing my second version of the class?
Without preparation on your part, you may need to resort to a hack: when your
public myClassdeserialization constructor gets the value ofbool d, enclose the code intry/catch, and setdto its default value when you catch an exception.In the future, add an
intvalue"__ver"(or any other name that does not collide with arguments that you pass toinfo.AddValue), and set it to a constant that you maintain in your class to indicate compatible and incompatible changes in serialization: