-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReflectionExtensions.cs
More file actions
221 lines (169 loc) · 6.71 KB
/
ReflectionExtensions.cs
File metadata and controls
221 lines (169 loc) · 6.71 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
namespace Pluton.Core
{
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
public static class ReflectionExtensions
{
//Instanced
public static object CallMethod(this object obj, string methodName, params object[] args)
{
var metInf = GetMethodInfo(obj, methodName);
if (metInf == null)
throw new Exception(String.Format("Couldn't find method '{0}' using reflection.", methodName));
if (metInf is MethodInfo)
return metInf.As<MethodInfo>().Invoke(obj, args);
return (object)null;
}
public static object CallMethodOnBase(this object obj, string methodName, params object[] args)
{
Type Base = obj.GetType().BaseType;
if (Base != null) {
return CallMethodOnBase(obj, GetMethodInfo(Base, methodName), args);
}
return null;
}
public static object CallMethodOnBase(this object obj, Type Base, string methodname, params object[] args)
{
return CallMethodOnBase(obj, GetMethodInfo(Base, methodname), args);
}
public static object CallMethodOnBase(this object obj, MethodInfo method, params object[] args)
{
var parameters = method.GetParameters();
if (parameters.Length == 0) {
if (args != null && args.Length != 0)
throw new Exception("Arguments count doesn't match");
} else {
if (parameters.Length != args.Length)
throw new Exception("Arguments count doesn't match");
}
Type returnType = null;
if (method.ReturnType != typeof(void)) {
returnType = method.ReturnType;
}
var type = obj.GetType();
var dynamicMethod = new DynamicMethod("", returnType,
new Type[] { type, typeof(Object) }, type);
var iLGenerator = dynamicMethod.GetILGenerator();
iLGenerator.Emit(OpCodes.Ldarg_0);
for (var i = 0; i < parameters.Length; i++) {
var parameter = parameters[i];
iLGenerator.Emit(OpCodes.Ldarg_1);
iLGenerator.Emit(OpCodes.Ldc_I4_S, i);
iLGenerator.Emit(OpCodes.Ldelem_Ref);
var parameterType = parameter.ParameterType;
if (parameterType.IsPrimitive) {
iLGenerator.Emit(OpCodes.Unbox_Any, parameterType);
} else if (parameterType == typeof(Object)) {
} else {
iLGenerator.Emit(OpCodes.Castclass, parameterType);
}
}
iLGenerator.Emit(OpCodes.Call, method);
iLGenerator.Emit(OpCodes.Ret);
return dynamicMethod.Invoke(null, new[] { obj, args });
}
public static object GetFieldValue(this object obj, string fieldName)
{
var memInf = GetFieldInfo(obj, fieldName);
if (memInf == null)
throw new Exception(String.Format("Couldn't find field '{0}' using reflection.", fieldName));
if (memInf is System.Reflection.PropertyInfo)
return memInf.As<System.Reflection.PropertyInfo>().GetValue(obj, null);
if (memInf is System.Reflection.FieldInfo)
return memInf.As<System.Reflection.FieldInfo>().GetValue(obj);
throw new Exception();
}
public static object GetFieldValueChain(this object obj, params string[] args)
{
foreach (string arg in args) {
obj = obj.GetFieldValue(arg);
}
return obj;
}
public static void SetFieldValue(this object obj, string fieldName, object newValue)
{
var memInf = GetFieldInfo(obj, fieldName);
if (memInf == null)
throw new Exception(String.Format("Couldn't find field '{0}' using reflection.", fieldName));
if (memInf is System.Reflection.PropertyInfo)
memInf.As<System.Reflection.PropertyInfo>().SetValue(obj, newValue, null);
else if (memInf is System.Reflection.FieldInfo)
memInf.As<System.Reflection.FieldInfo>().SetValue(obj, newValue);
else
throw new System.Exception();
}
private static MethodInfo GetMethodInfo(Type classType, string methodName)
{
return classType.GetMethod(methodName,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.Static);
}
private static MethodInfo GetMethodInfo(object obj, string methodName)
{
return GetMethodInfo(obj.GetType(), methodName);
}
private static MemberInfo GetFieldInfo(Type objType, string fieldName)
{
var prps = new List<System.Reflection.PropertyInfo>();
prps.Add(objType.GetProperty(fieldName,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.Static));
prps = System.Linq.Enumerable.ToList(System.Linq.Enumerable.Where(prps, i => !ReferenceEquals(i, null)));
if (prps.Count != 0)
return prps[0];
var flds = new System.Collections.Generic.List<System.Reflection.FieldInfo>();
flds.Add(objType.GetField(fieldName,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.Static));
flds = System.Linq.Enumerable.ToList(System.Linq.Enumerable.Where(flds, i => !ReferenceEquals(i, null)));
if (flds.Count != 0)
return flds[0];
// if not found on the current type, check the base
if (objType.BaseType != null) {
return GetFieldInfo(objType.BaseType, fieldName);
}
return null;
}
private static MemberInfo GetFieldInfo(object obj, string fieldName)
{
return GetFieldInfo(obj.GetType(), fieldName);
}
//Static
public static object CallStaticMethod(this Type classType, string methodName, params object[] args)
{
var metInf = GetMethodInfo(classType, methodName);
if (metInf == null)
throw new Exception(String.Format("Couldn't find method '{0}' using reflection.", methodName));
if (metInf is MethodInfo)
return metInf.As<MethodInfo>().Invoke(null, args);
return null;
}
public static object GetStaticFieldValue(this Type classType, string fieldName)
{
var memInf = GetFieldInfo(classType, fieldName);
if (memInf == null)
throw new Exception(String.Format("Couldn't find field '{0}' using reflection.", fieldName));
if (memInf is System.Reflection.PropertyInfo)
return memInf.As<System.Reflection.PropertyInfo>().GetValue(null, null);
if (memInf is System.Reflection.FieldInfo)
return memInf.As<System.Reflection.FieldInfo>().GetValue(null);
throw new Exception();
}
public static void SetFieldValueValue(this Type classType, string fieldName, object newValue)
{
var memInf = GetFieldInfo(classType, fieldName);
if (memInf == null)
throw new Exception(String.Format("Couldn't find field '{0}' using reflection.", fieldName));
if (memInf is System.Reflection.PropertyInfo)
memInf.As<System.Reflection.PropertyInfo>().SetValue(null, newValue, null);
else if (memInf is System.Reflection.FieldInfo)
memInf.As<System.Reflection.FieldInfo>().SetValue(null, newValue);
else
throw new System.Exception();
}
[System.Diagnostics.DebuggerHidden]
private static T As<T>(this object obj)
{
return (T)obj;
}
}
}