I have one class implementing ISerializable interface, with one private field that is saved and retrieved:
Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
_name= info.GetString("_name")
End Sub
Now, I want to add a new private field, but since it did not exist previously, when I try to open a serialized object I get an exception when trying to get this field, in the custom deserialization constructor. The solution I've come up with is to use a Try...Catch block, as follows:
Protected Sub New(ByVal info As SerializationInfo, _
ByVal context As StreamingContext)
_name= info.GetString("_name")
Try
_active= info.GetBoolean("_active")
Catch ex As Exception
_active= False
End Try
End Sub
It works, but it seems a bit messy to me, moreover because I will have to add a new Try...Catch block for each new field I want to add. Is there any better way of doing this?
Thanks!
EDIT: In this answer t0mm13b says that "There is a more neater way to do this in 2.0+ upwards". Any ideas about what is he talking about?