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