forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionHelpers.cs
More file actions
185 lines (155 loc) · 4.16 KB
/
ReflectionHelpers.cs
File metadata and controls
185 lines (155 loc) · 4.16 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
using System;
using System.Linq;
using System.Reflection;
using JavaScriptEngineSwitcher.Core.Utilities;
namespace JavaScriptEngineSwitcher.ChakraCore.Helpers
{
/// <summary>
/// Reflection helpers
/// </summary>
internal static class ReflectionHelpers
{
public static BindingFlags GetDefaultBindingFlags(bool instance)
{
BindingFlags bindingFlags = BindingFlags.Public;
if (instance)
{
bindingFlags |= BindingFlags.Instance;
}
else
{
bindingFlags |= BindingFlags.Static;
}
return bindingFlags;
}
public static void FixFieldValueType(ref object value, FieldInfo field)
{
Type valueType = value.GetType();
Type fieldType = field.FieldType;
if (valueType != fieldType)
{
object convertedValue;
if (TypeConverter.TryConvertToType(value, fieldType, out convertedValue))
{
value = convertedValue;
}
}
}
public static void FixPropertyValueType(ref object value, PropertyInfo property)
{
Type valueType = value.GetType();
Type propertyType = property.PropertyType;
if (valueType != propertyType)
{
object convertedValue;
if (TypeConverter.TryConvertToType(value, propertyType, out convertedValue))
{
value = convertedValue;
}
}
}
public static void FixArgumentTypes(ref object[] argValues, ParameterInfo[] parameters)
{
int argCount = argValues.Length;
for (int argIndex = 0; argIndex < argCount; argIndex++)
{
object argValue = argValues[argIndex];
Type argType = argValue.GetType();
ParameterInfo parameter = parameters[argIndex];
Type parameterType = parameter.ParameterType;
if (argType != parameterType)
{
object convertedArgValue;
if (TypeConverter.TryConvertToType(argValue, parameterType, out convertedArgValue))
{
argValues[argIndex] = convertedArgValue;
}
}
}
}
public static MethodBase GetBestFitMethod(MethodBase[] methods, object[] argValues)
{
int argCount = argValues.Length;
var methodCandidates = methods
.Select(m => new
{
Method = m,
ParameterTypes = m.GetParameters()
.Select(p => p.ParameterType)
.ToArray()
})
.ToArray()
;
var methodsWithSameArity = methodCandidates
.Where(m => m.ParameterTypes.Length == argCount)
.ToArray()
;
if (methodsWithSameArity.Length == 0)
{
return null;
}
Type[] argTypes = argValues
.Select(a => a.GetType())
.ToArray()
;
var weaklyCompatibleMethods = methodsWithSameArity
.Where(m => CompareParameterTypes(argValues, argTypes, m.ParameterTypes, false))
.ToArray()
;
int weaklyCompatibleMethodCount = weaklyCompatibleMethods.Length;
if (weaklyCompatibleMethodCount > 0)
{
if (weaklyCompatibleMethodCount == 1)
{
return weaklyCompatibleMethods[0].Method;
}
var strictlyCompatibleMethods = weaklyCompatibleMethods
.Where(m => CompareParameterTypes(argValues, argTypes, m.ParameterTypes, true))
.ToArray()
;
if (strictlyCompatibleMethods.Length > 0)
{
return strictlyCompatibleMethods[0].Method;
}
return weaklyCompatibleMethods[0].Method;
}
return null;
}
private static bool CompareParameterTypes(object[] argValues, Type[] argTypes, Type[] parameterTypes,
bool strictСompliance)
{
int argValueCount = argValues.Length;
int argTypeCount = argTypes.Length;
int parameterCount = parameterTypes.Length;
if (argValueCount != argTypeCount || argTypeCount != parameterCount)
{
return false;
}
for (int argIndex = 0; argIndex < argValueCount; argIndex++)
{
object argValue = argValues[argIndex];
Type argType = argTypes[argIndex];
Type parameterType = parameterTypes[argIndex];
if (argType != parameterType)
{
if (!strictСompliance
&& NumericHelpers.IsNumericType(argType) && NumericHelpers.IsNumericType(parameterType))
{
object convertedArgValue;
if (!TypeConverter.TryConvertToType(argValue, parameterType, out convertedArgValue))
{
return false;
}
if (argValue != convertedArgValue)
{
return false;
}
continue;
}
return false;
}
}
return true;
}
}
}