Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2287 - 1

2
namespace GMap.NET
3
{
4
   using System;
5
   using System.Runtime.Serialization;
6
   using System.Diagnostics;
7
 
8
   public static class Extensions
9
   {
10
      /// <summary>
11
      /// Retrieves a value from the SerializationInfo of the given type.
12
      /// </summary>
13
      /// <typeparam name="T">The Type that we are attempting to de-serialize.</typeparam>
14
      /// <param name="info">The SerializationInfo.</param>
15
      /// <param name="key">The key of the value we wish to retrieve.</param>
16
      /// <returns>The value if found, otherwise null.</returns>
17
      public static T GetValue<T>(SerializationInfo info, string key) where T : class
18
      {
19
         try
20
         {
21
            // Return the value from the SerializationInfo, casting it to type T.
22
            return info.GetValue(key, typeof(T)) as T;
23
         }
24
         catch(Exception ex)
25
         {
26
            Debug.WriteLine("Extensions.GetValue: " + ex.Message);
27
            return null;
28
         }
29
      }
30
 
31
      /// <summary>
32
      /// Retrieves a value from the SerializationInfo of the given type.
33
      /// </summary>
34
      /// <typeparam name="T">The Type that we are attempting to de-serialize.</typeparam>
35
      /// <param name="info">The SerializationInfo.</param>
36
      /// <param name="key">The key of the value we wish to retrieve.</param>
37
      /// <param name="defaultValue">The default value if the de-serialized value was null.</param>
38
      /// <returns>The value if found, otherwise the default value.</returns>
39
      public static T GetValue<T>(SerializationInfo info, string key, T defaultValue) where T : class
40
      {
41
         T deserializedValue = GetValue<T>(info, key);
42
         if(deserializedValue != null)
43
         {
44
            return deserializedValue;
45
         }
46
 
47
         return defaultValue;
48
      }
49
 
50
      /// <summary>
51
      /// Retrieves a value from the SerializationInfo of the given type for structs.
52
      /// </summary>
53
      /// <typeparam name="T">The Type that we are attempting to de-serialize.</typeparam>
54
      /// <param name="info">The SerializationInfo.</param>
55
      /// <param name="key">The key of the value we wish to retrieve.</param>
56
      /// <param name="defaultValue">The default value if the de-serialized value was null.</param>
57
      /// <returns>The value if found, otherwise the default value.</returns>
58
      public static T GetStruct<T>(SerializationInfo info, string key, T defaultValue) where T : struct
59
      {
60
         try
61
         {
62
            return (T)info.GetValue(key, typeof(T));
63
         }
64
         catch(Exception ex)
65
         {
66
            Debug.WriteLine("Extensions.GetStruct: " + ex.Message);
67
            return defaultValue;
68
         }
69
      }
70
 
71
      /// <summary>
72
      /// Retrieves a value from the SerializationInfo of the given type for structs.
73
      /// </summary>
74
      /// <typeparam name="T">The Type that we are attempting to de-serialize.</typeparam>
75
      /// <param name="info">The SerializationInfo.</param>
76
      /// <param name="key">The key of the value we wish to retrieve.</param>
77
      /// <param name="defaultValue">The default value if the de-serialized value was null.</param>
78
      /// <returns>The value if found, otherwise the default value.</returns>
79
      public static Nullable<T> GetStruct<T>(SerializationInfo info, string key, Nullable<T> defaultValue) where T : struct
80
      {
81
         try
82
         {
83
            return (Nullable<T>)info.GetValue(key, typeof(Nullable<T>));
84
         }
85
         catch(Exception ex)
86
         {
87
            Debug.WriteLine("Extensions.GetStruct: " + ex.Message);
88
            return defaultValue;
89
         }
90
      }
91
   }
92
}