forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeConverter.cs
More file actions
200 lines (173 loc) · 4.89 KB
/
Copy pathTypeConverter.cs
File metadata and controls
200 lines (173 loc) · 4.89 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.ComponentModel;
using System.Globalization;
#if NET45 || NETSTANDARD
using System.Reflection;
#endif
using OriginalTypeConverter = System.ComponentModel.TypeConverter;
#if NETSTANDARD1_3
using JavaScriptEngineSwitcher.Core.Polyfills.System.Reflection;
#endif
using JavaScriptEngineSwitcher.Core.Resources;
namespace JavaScriptEngineSwitcher.Core.Utilities
{
/// <summary>
/// Type converter
/// </summary>
public static class TypeConverter
{
/// <summary>
/// Converts the specified value to the specified type
/// </summary>
/// <typeparam name="T">The type to convert the value to</typeparam>
/// <param name="value">The value to convert</param>
/// <returns>The value that has been converted to the target type</returns>
public static T ConvertToType<T>(object value)
{
return (T)ConvertToType(value, typeof(T));
}
/// <summary>
/// Converts the specified value to the specified type
/// </summary>
/// <param name="value">The value to convert</param>
/// <param name="targetType">The type to convert the value to</param>
/// <returns>The value that has been converted to the target type</returns>
public static object ConvertToType(object value, Type targetType)
{
object result;
ConvertObjectToType(value, targetType, true, out result);
return result;
}
/// <summary>
/// Converts the specified value to the specified type.
/// A return value indicates whether the conversion succeeded.
/// </summary>
/// <typeparam name="T">The type to convert the value to</typeparam>
/// <param name="value">The value to convert</param>
/// <param name="convertedValue">The value that has been converted to the target type</param>
/// <returns>Result of conversion (true - success; false - failure)</returns>
public static bool TryConvertToType<T>(object value, out T convertedValue)
{
object resultValue;
bool result = TryConvertToType(value, typeof(T), out resultValue);
convertedValue = (T)resultValue;
return result;
}
/// <summary>
/// Converts the specified value to the specified type.
/// A return value indicates whether the conversion succeeded.
/// </summary>
/// <param name="value">The value to convert</param>
/// <param name="targetType">The type to convert the value to</param>
/// <param name="convertedValue">The value that has been converted to the target type</param>
/// <returns>Result of conversion (true - success; false - failure)</returns>
public static bool TryConvertToType(object value, Type targetType, out object convertedValue)
{
bool result = ConvertObjectToType(value, targetType, false, out convertedValue);
return result;
}
private static bool ConvertObjectToType(object obj, Type type, bool throwOnError,
out object convertedObject)
{
if (obj == null)
{
if (IsNonNullableValueType(type))
{
if (throwOnError)
{
throw new InvalidOperationException(Strings.Common_ValueTypeCannotBeNull);
}
convertedObject = null;
return false;
}
convertedObject = null;
return true;
}
if (type != null && obj.GetType() != type)
{
return InnerConvertObjectToType(obj, type, throwOnError, out convertedObject);
}
convertedObject = obj;
return true;
}
private static bool InnerConvertObjectToType(object obj, Type type, bool throwOnError,
out object convertedObject)
{
Type originalType = obj.GetType();
OriginalTypeConverter converter = TypeDescriptor.GetConverter(type);
if (converter.CanConvertFrom(originalType))
{
try
{
convertedObject = converter.ConvertFrom(null, CultureInfo.InvariantCulture, obj);
return true;
}
catch
{
if (throwOnError)
{
throw;
}
convertedObject = null;
return false;
}
}
if (converter.CanConvertFrom(typeof(string)) && !(obj is DateTime))
{
try
{
string text = TypeDescriptor.GetConverter(originalType).ConvertToInvariantString(obj);
convertedObject = converter.ConvertFromInvariantString(text);
return true;
}
catch
{
if (throwOnError)
{
throw;
}
convertedObject = null;
return false;
}
}
#if NET40
if (type.IsInstanceOfType(obj))
#else
if (type.GetTypeInfo().IsInstanceOfType(obj))
#endif
{
convertedObject = obj;
return true;
}
if (throwOnError)
{
throw new InvalidOperationException(
string.Format(Strings.Common_CannotConvertObjectToType, originalType, type)
);
}
convertedObject = null;
return false;
}
private static bool IsNonNullableValueType(Type type)
{
if (type == null)
{
return false;
}
#if NET40
Type typeInfo = type;
#else
TypeInfo typeInfo = type.GetTypeInfo();
#endif
if (!typeInfo.IsValueType)
{
return false;
}
if (typeInfo.IsGenericType)
{
return type.GetGenericTypeDefinition() != typeof(Nullable<>);
}
return true;
}
}
}