-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathInvokeMethod.cs
More file actions
200 lines (179 loc) · 4.96 KB
/
Copy pathInvokeMethod.cs
File metadata and controls
200 lines (179 loc) · 4.96 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
#region License
/*
* NReco Lambda Parser (http://www.nrecosite.com/)
* Copyright 2014-2016 Vitaliy Fedorchenko
* Distributed under the MIT license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Reflection;
namespace NReco
{
/// <summary>
/// Invoke object's method that is most compatible with provided arguments
/// </summary>
internal class InvokeMethod
{
public object TargetObject { get; set; }
public string MethodName { get; set; }
public InvokeMethod(object o, string methodName)
{
TargetObject = o;
MethodName = methodName;
}
protected MethodInfo FindMethod(Type[] argTypes)
{
if (TargetObject is Type)
{
// static method
#if NET40
return ((Type)TargetObject).GetMethod(MethodName, BindingFlags.Static | BindingFlags.Public);
#else
return ((Type) TargetObject).GetRuntimeMethod(MethodName, argTypes);
#endif
}
#if NET40
return TargetObject.GetType().GetMethod(MethodName, argTypes);
#else
return TargetObject.GetType().GetRuntimeMethod(MethodName, argTypes);
#endif
}
protected IEnumerable<MethodInfo> GetAllMethods()
{
if (TargetObject is Type)
{
#if NET40
return ((Type)TargetObject).GetMethods(BindingFlags.Static | BindingFlags.Public);
#else
return ((Type) TargetObject).GetRuntimeMethods();
#endif
}
#if NET40
return TargetObject.GetType().GetMethods();
#else
return TargetObject.GetType().GetRuntimeMethods();
#endif
}
public object Invoke(object[] args)
{
Type[] argTypes = new Type[args.Length];
for (int i = 0; i < argTypes.Length; i++)
argTypes[i] = args[i] != null ? args[i].GetType() : typeof(object);
// strict matching first
MethodInfo targetMethodInfo = FindMethod(argTypes);
// fuzzy matching
if (targetMethodInfo == null)
{
var methods = GetAllMethods();
foreach (var m in methods)
if (m.Name == MethodName &&
m.GetParameters().Length == args.Length &&
CheckParamsCompatibility(m.GetParameters(), argTypes, args))
{
targetMethodInfo = m;
break;
}
}
if (targetMethodInfo == null)
{
string[] argTypeNames = new string[argTypes.Length];
for (int i = 0; i < argTypeNames.Length; i++)
argTypeNames[i] = argTypes[i].Name;
string argTypeNamesStr = String.Join(",", argTypeNames);
throw new MissingMemberException(
(TargetObject is Type ? (Type) TargetObject : TargetObject.GetType()).FullName + "." + MethodName);
}
object[] argValues = PrepareActualValues(targetMethodInfo.GetParameters(), args);
object res = null;
try
{
res = targetMethodInfo.Invoke(TargetObject is Type ? null : TargetObject, argValues);
}
catch (TargetInvocationException tiEx)
{
if (tiEx.InnerException != null)
throw new Exception(tiEx.InnerException.Message, tiEx.InnerException);
else
{
throw;
}
}
return res;
}
internal static bool IsInstanceOfType(Type t, object val)
{
#if NET40
return t.IsInstanceOfType(val);
#else
return val != null && t.GetTypeInfo().IsAssignableFrom(val.GetType().GetTypeInfo());
#endif
}
protected bool CheckParamsCompatibility(ParameterInfo[] paramsInfo, Type[] types, object[] values)
{
for (int i = 0; i < paramsInfo.Length; i++)
{
Type paramType = paramsInfo[i].ParameterType;
var val = values[i];
if (IsInstanceOfType(paramType, val))
continue;
// null and reference types
if (val == null &&
#if NET40
!paramType.IsValueType
#else
!paramType.GetTypeInfo().IsValueType
#endif
)
continue;
// possible autocast between generic/non-generic common types
try
{
Convert.ChangeType(val, paramType, System.Globalization.CultureInfo.InvariantCulture);
continue;
}
catch
{
}
//if (ConvertManager.CanChangeType(types[i],paramType))
// continue;
// incompatible parameter
return false;
}
return true;
}
protected object[] PrepareActualValues(ParameterInfo[] paramsInfo, object[] values)
{
object[] res = new object[paramsInfo.Length];
for (int i = 0; i < paramsInfo.Length; i++)
{
if (values[i] == null || IsInstanceOfType(paramsInfo[i].ParameterType, values[i]))
{
res[i] = values[i];
continue;
}
try
{
res[i] = Convert.ChangeType(values[i], paramsInfo[i].ParameterType,
System.Globalization.CultureInfo.InvariantCulture);
continue;
}
catch
{
throw new InvalidCastException(
String.Format("Invoke method '{0}': cannot convert argument #{1} from {2} to {3}",
MethodName, i, values[i].GetType(), paramsInfo[i].ParameterType));
}
}
return res;
}
}
}