Lets say i have a class
[Serializable()]
public class Car
{
public string model;
public int year;
}
I serialize that to disk named “car.xx”. Then i add a property to my Car class so it will be like this:
[Serializable()]
public class Car
{
public string model;
public int year;
public string colour;
}
Then i deserialize “car.xx” ( that contains 2 fields ) to my current car class that contains 3 fields, which will make the “colour” property of our Car class null.
How do i set that “new properties” dont get null values? Setting them in the constructor wont help.
I am using BinaryFormatter serializer
I want string values that are null to be replaced with “”
if you are not using XmlSerializer then consider using OnDeserializedAttribute, OnSerializingAttribute, OnSerializedAttribute, and OnDeserializingAttribute attributes
see this.
like: