forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeExtensions.cs
More file actions
102 lines (99 loc) · 1.99 KB
/
Copy pathTypeExtensions.cs
File metadata and controls
102 lines (99 loc) · 1.99 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
using System;
#if NETSTANDARD1_3
using System.Reflection;
#endif
namespace JavaScriptEngineSwitcher.Core.Extensions
{
/// <summary>
/// Type extensions
/// </summary>
public static class TypeExtensions
{
/// <summary>
/// Gets a underlying type code of the specified <see cref="Type"/>
/// </summary>
/// <param name="source">The type whose underlying type code to get</param>
/// <returns>The code of the underlying type</returns>
public static TypeCode GetTypeCode(this Type source)
{
TypeCode typeCode;
#if NETSTANDARD1_3
if (source == null)
{
typeCode = TypeCode.Empty;
}
else if (source == typeof(bool))
{
typeCode = TypeCode.Boolean;
}
else if (source == typeof(char))
{
typeCode = TypeCode.Char;
}
else if (source == typeof(sbyte))
{
typeCode = TypeCode.SByte;
}
else if (source == typeof(byte))
{
typeCode = TypeCode.Byte;
}
else if (source == typeof(short))
{
typeCode = TypeCode.Int16;
}
else if (source == typeof(ushort))
{
typeCode = TypeCode.UInt16;
}
else if (source == typeof(int))
{
typeCode = TypeCode.Int32;
}
else if (source == typeof(uint))
{
typeCode = TypeCode.UInt32;
}
else if (source == typeof(long))
{
typeCode = TypeCode.Int64;
}
else if (source == typeof(ulong))
{
typeCode = TypeCode.UInt64;
}
else if (source == typeof(float))
{
typeCode = TypeCode.Single;
}
else if (source == typeof(double))
{
typeCode = TypeCode.Double;
}
else if (source == typeof(decimal))
{
typeCode = TypeCode.Decimal;
}
else if (source == typeof(DateTime))
{
typeCode = TypeCode.DateTime;
}
else if (source == typeof(string))
{
typeCode = TypeCode.String;
}
else if (source.GetTypeInfo().IsEnum)
{
typeCode = GetTypeCode(Enum.GetUnderlyingType(source));
}
else
{
typeCode = TypeCode.Object;
}
#else
typeCode = Type.GetTypeCode(source);
#endif
return typeCode;
}
}
}