Say I created a data class some time ago:
[Serializable]
public class MyData
{
public string name;
public string nickname;
public int id;
}
And I used BinaryFormatter to serialize and save users' data to a file. After time has passed, I decide we no longer need the field "nickname," so I remove it:
[Serializable]
public class MyData
{
public string name;
public int id;
}
However, now when I try to deserialize an object that was serialized when it still contained the nickname field, it throws an exception:
System.Runtime.Serialization.SerializationException: Field "nickname" not found in class MyData
Is there a way to tell the BinaryFormatter that if it encounters the field nickname to just ignore it and deserialize the rest of the fields as normal?
No, this is impossible. What you have to do is deserialize it to a class with the same structure as before, then manually migrate your old object to your new object, and save the new object for the future.