When implementing the ISerializable
interface in C#, we provide a constructor which takes a SerializationInfo
object, and then queries it with various
Well no one answered 'why', but I'm guessing that's addressed to MS..
My implementation for anyone in need:
public static class SerializationInfoExtensions
{
public static bool TryGetValue(this SerializationInfo serializationInfo, string name, out T value)
{
try
{
value = (T) serializationInfo.GetValue(name, typeof(T));
return true;
}
catch (SerializationException)
{
value = default(T);
return false;
}
}
public static T GetValueOrDefault(this SerializationInfo serializationInfo, string name, Lazy defaultValue)
{
try
{
return (T) serializationInfo.GetValue(name, typeof(T));
}
catch (SerializationException)
{
return defaultValue.Value;
}
}
}