namespace GMap.NET { using System; using System.Runtime.Serialization; using System.Diagnostics; public static class Extensions { /// /// Retrieves a value from the SerializationInfo of the given type. /// /// The Type that we are attempting to de-serialize. /// The SerializationInfo. /// The key of the value we wish to retrieve. /// The value if found, otherwise null. public static T GetValue(SerializationInfo info, string key) where T : class { try { // Return the value from the SerializationInfo, casting it to type T. return info.GetValue(key, typeof(T)) as T; } catch(Exception ex) { Debug.WriteLine("Extensions.GetValue: " + ex.Message); return null; } } /// /// Retrieves a value from the SerializationInfo of the given type. /// /// The Type that we are attempting to de-serialize. /// The SerializationInfo. /// The key of the value we wish to retrieve. /// The default value if the de-serialized value was null. /// The value if found, otherwise the default value. public static T GetValue(SerializationInfo info, string key, T defaultValue) where T : class { T deserializedValue = GetValue(info, key); if(deserializedValue != null) { return deserializedValue; } return defaultValue; } /// /// Retrieves a value from the SerializationInfo of the given type for structs. /// /// The Type that we are attempting to de-serialize. /// The SerializationInfo. /// The key of the value we wish to retrieve. /// The default value if the de-serialized value was null. /// The value if found, otherwise the default value. public static T GetStruct(SerializationInfo info, string key, T defaultValue) where T : struct { try { return (T)info.GetValue(key, typeof(T)); } catch(Exception ex) { Debug.WriteLine("Extensions.GetStruct: " + ex.Message); return defaultValue; } } /// /// Retrieves a value from the SerializationInfo of the given type for structs. /// /// The Type that we are attempting to de-serialize. /// The SerializationInfo. /// The key of the value we wish to retrieve. /// The default value if the de-serialized value was null. /// The value if found, otherwise the default value. public static Nullable GetStruct(SerializationInfo info, string key, Nullable defaultValue) where T : struct { try { return (Nullable)info.GetValue(key, typeof(Nullable)); } catch(Exception ex) { Debug.WriteLine("Extensions.GetStruct: " + ex.Message); return defaultValue; } } } }