-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeUtils.cs
More file actions
180 lines (162 loc) · 6.79 KB
/
TypeUtils.cs
File metadata and controls
180 lines (162 loc) · 6.79 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
using System.Reflection;
namespace Ramstack.Parsing.Internal;
/// <summary>
/// Provides type utility methods.
/// </summary>
public static class TypeUtils
{
private static ReadOnlySpan<Primitives> PrimitiveConversions =>
[
/* Empty */ 0,
/* Object */ 0,
/* DBNull */ 0,
/* Boolean */ Primitives.Boolean,
/* Char */ Primitives.Char | Primitives.UInt16 | Primitives.UInt32 | Primitives.Int32 | Primitives.UInt64 | Primitives.Int64 | Primitives.Single | Primitives.Double,
/* SByte */ Primitives.SByte | Primitives.Int16 | Primitives.Int32 | Primitives.Int64 | Primitives.Single | Primitives.Double,
/* Byte */ Primitives.Byte | Primitives.Char | Primitives.UInt16 | Primitives.Int16 | Primitives.UInt32 | Primitives.Int32 | Primitives.UInt64 | Primitives.Int64 | Primitives.Single | Primitives.Double,
/* Int16 */ Primitives.Int16 | Primitives.Int32 | Primitives.Int64 | Primitives.Single | Primitives.Double,
/* UInt16 */ Primitives.UInt16 | Primitives.UInt32 | Primitives.Int32 | Primitives.UInt64 | Primitives.Int64 | Primitives.Single | Primitives.Double,
/* Int32 */ Primitives.Int32 | Primitives.Int64 | Primitives.Single | Primitives.Double,
/* UInt32 */ Primitives.UInt32 | Primitives.UInt64 | Primitives.Int64 | Primitives.Single | Primitives.Double,
/* Int64 */ Primitives.Int64 | Primitives.Single | Primitives.Double,
/* UInt64 */ Primitives.UInt64 | Primitives.Single | Primitives.Double,
/* Single */ Primitives.Single | Primitives.Double,
/* Double */ Primitives.Double,
/* Decimal */ Primitives.Decimal,
/* DateTime */ Primitives.DateTime,
/* Unknown */ 0,
/* String */ Primitives.String
];
/// <summary>
/// Determines whether the <paramref name="type"/> is a numeric type.
/// </summary>
/// <param name="type">The <see cref="Type"/> to test.</param>
/// <returns>
/// A value indicating whether the <paramref name="type"/> is a numeric type.
/// </returns>
public static bool IsNumeric(Type type)
{
if (!type.IsEnum)
{
switch (Type.GetTypeCode(type))
{
case TypeCode.Char:
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Double:
case TypeCode.Single:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Decimal:
return true;
}
}
return false;
}
/// <summary>
/// Determines whether the specified type represents an integer type.
/// </summary>
/// <param name="type">The <see cref="Type"/> to evaluate.</param>
/// <returns>
/// <c>true</c> if the type is an integer type (signed or unsigned); otherwise, <c>false</c>.
/// </returns>
/// <remarks>
/// This method checks for the following integer types:
/// <list type="bullet">
/// <item><description><see cref="TypeCode.SByte"/></description></item>
/// <item><description><see cref="TypeCode.Byte"/></description></item>
/// <item><description><see cref="TypeCode.Int16"/></description></item>
/// <item><description><see cref="TypeCode.UInt16"/></description></item>
/// <item><description><see cref="TypeCode.Int32"/></description></item>
/// <item><description><see cref="TypeCode.UInt32"/></description></item>
/// <item><description><see cref="TypeCode.Int64"/></description></item>
/// <item><description><see cref="TypeCode.UInt64"/></description></item>
/// </list>
/// </remarks>
public static bool IsInteger(Type type)
{
if (!type.IsEnum)
{
switch (Type.GetTypeCode(type))
{
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
return true;
}
}
return false;
}
/// <summary>
/// Determines whether the <paramref name="source"/> can convert to the <paramref name="target"/>.
/// </summary>
/// <param name="source">The type to convert from.</param>
/// <param name="target">The type to convert to.</param>
/// <returns>
/// A value indicating whether the conversion is possible.
/// </returns>
public static bool CanConvertPrimitive(Type source, Type target)
{
if (source.IsEnum)
return false;
if ((source == typeof(IntPtr) && target == typeof(IntPtr))
|| (source == typeof(UIntPtr) && target == typeof(UIntPtr)))
return true;
var s = PrimitiveConversions[(int)Type.GetTypeCode(source)];
var t = (Primitives)(1 << (int)Type.GetTypeCode(target));
return (s & t) != 0;
}
/// <summary>
/// Attempts to find an implicit conversion operator.
/// </summary>
/// <param name="source">The type to convert from.</param>
/// <param name="target">The type to convert to.</param>
/// <returns>
/// The conversion operator.
/// </returns>
public static MethodInfo? FindConversionOperator(Type source, Type target)
{
var methods = source.GetMethods(
BindingFlags.Public
| BindingFlags.NonPublic
| BindingFlags.Static);
foreach (var mi in methods)
{
if (mi.Name != "op_Implicit" || mi.ReturnType != target)
continue;
if (mi.GetParameters()[0].ParameterType == source)
return mi;
}
return null;
}
#region Inner type: Primitives
[Flags]
private enum Primitives
{
Boolean = 1 << TypeCode.Boolean,
Char = 1 << TypeCode.Char,
SByte = 1 << TypeCode.SByte,
Byte = 1 << TypeCode.Byte,
Int16 = 1 << TypeCode.Int16,
UInt16 = 1 << TypeCode.UInt16,
Int32 = 1 << TypeCode.Int32,
UInt32 = 1 << TypeCode.UInt32,
Int64 = 1 << TypeCode.Int64,
UInt64 = 1 << TypeCode.UInt64,
Single = 1 << TypeCode.Single,
Double = 1 << TypeCode.Double,
Decimal = 1 << TypeCode.Decimal,
DateTime = 1 << TypeCode.DateTime,
String = 1 << TypeCode.String
}
#endregion
}