When implementing the ISerializable
interface in C#, we provide a constructor which takes a SerializationInfo
object, and then queries it with various GetInt32
, GetObject
etc. methods in order to fill the fields of the object which we are trying to deserialize.
One major reason to implement this interface, rather than just using the [Serializable]
attribute, is for backwards compatibility: if we have added new fields to the class at some point, we can catch the SerializationException
thrown by a serialized, older version of the class, and handle them in an appropriate manner.
My question is the following: why do we have to use these exceptions for what is, essentially, control flow? If I am deserializing a large number of classes which were saved some time ago, potentially each missing field in each class will throw an exception, causing really bad performance.
Why does the SerializationInfo
class not provide TryGetValue
methods which would simply return false
if the name string were not present?