forked from IS4Code/SharpUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHacks.cs
259 lines (233 loc) · 7.67 KB
/
Hacks.cs
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* Date: 6.1.2015, Time: 20:42 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.Serialization;
using IllidanS4.SharpUtils.Reflection;
using IllidanS4.SharpUtils.Reflection.Emit;
namespace IllidanS4.SharpUtils
{
public sealed class Hacks : HacksBase<MulticastDelegate>
{
private Hacks(){}
}
public abstract class HacksBase<TDelegateBase> where TDelegateBase : class, ICloneable, ISerializable
{
internal HacksBase(){}
const BindingFlags privflags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
public static TDelegate GetInvoker<TDelegate>(Type type, string method, bool instance, int ord=0) where TDelegate : TDelegateBase
{
BindingFlags flags = privflags;
flags &= instance ? ~BindingFlags.Static : ~BindingFlags.Instance;
MethodInfo mi;
try{
mi = type.GetMethod(method, flags);
}catch(AmbiguousMatchException)
{
var tParams = typeof(TDelegate).GetDelegateSignature().ParameterTypes;
if(instance == true)
{
tParams = tParams.Skip(1).ToArray();
}
mi = type.GetMethod(method, flags, null, 0, tParams, null);
if(mi == null) mi = type.GetMethods(flags).Where(m => m.Name == method).ElementAt(ord);
}
return GetInvoker<TDelegate>(mi);
}
public static TDelegate GetInvoker<TDelegate>(MethodInfo mi) where TDelegate : TDelegateBase
{
Type delType = typeof(TDelegate);
MethodSignature delSig = delType.GetDelegateSignature();
Type[] tParams = delSig.ParameterTypes;
Type retType = delSig.ReturnType;
var mParams = mi.GetParameters().Select(p => p.ParameterType).ToList();
try{
return (TDelegate)(object)mi.CreateDelegate(delType);
}catch(ArgumentException)
{
}
DynamicMethod dyn = new DynamicMethod(mi.Name, retType, tParams, typeof(Hacks).Module, true);
var il = dyn.GetILGenerator();
int add = 0;
if(!mi.IsStatic)
{
add = 1;
EmitThis(il, tParams[0], mi.DeclaringType);
}
for(int i = add; i < tParams.Length; i++)
{
il.EmitLdarg(i);
EmitCast(il, tParams[i], mParams[i-add]);
}
il.Emit(OpCodes.Call, mi);
EmitCast(il, mi.ReturnType, retType);
il.Emit(OpCodes.Ret);
return (TDelegate)(object)dyn.CreateDelegate(delType);
}
public static TDelegate GetConstructor<TDelegate>(Type type, int ord) where TDelegate : TDelegateBase
{
return GetConstructor<TDelegate>(type.GetConstructors(privflags)[ord]);
}
public static TDelegate GetConstructor<TDelegate>(ConstructorInfo ctor) where TDelegate : TDelegateBase
{
Type delType = typeof(TDelegate);
MethodSignature delSig = delType.GetDelegateSignature();
Type[] tParams = delSig.ParameterTypes;
Type retType = delSig.ReturnType;
var mParams = ctor.GetParameters().Select(p => p.ParameterType).ToList();
DynamicMethod dyn = new DynamicMethod("new_"+ctor.DeclaringType.Name, retType, tParams, typeof(Hacks).Module, true);
var il = dyn.GetILGenerator();
for(int i = 0; i < tParams.Length; i++)
{
il.EmitLdarg(i);
EmitCast(il, tParams[i], mParams[i]);
}
il.Emit(OpCodes.Newobj, ctor);
EmitCast(il, ctor.DeclaringType, retType);
il.Emit(OpCodes.Ret);
return (TDelegate)(object)dyn.CreateDelegate(delType);
}
public static TDelegate GetFieldGetter<TDelegate>(Type type, string field) where TDelegate : TDelegateBase
{
return GetFieldGetter<TDelegate>(type.GetField(field, privflags));
}
public static TDelegate GetFieldGetter<TDelegate>(FieldInfo fi) where TDelegate : TDelegateBase
{
Type delType = typeof(TDelegate);
MethodSignature delSig = ReflectionTools.GetDelegateSignature(delType);
Type[] tParams = delSig.ParameterTypes;
Type retType = delSig.ReturnType;
DynamicMethod dyn = new DynamicMethod("get_"+fi.Name, retType, tParams, typeof(Hacks).Module, true);
var il = dyn.GetILGenerator();
if(fi.IsStatic)
{
il.Emit(OpCodes.Ldsfld, fi);
}else{
EmitThis(il, tParams[0], fi.DeclaringType);
il.Emit(OpCodes.Ldfld, fi);
}
EmitCast(il, fi.FieldType, retType);
il.Emit(OpCodes.Ret);
return (TDelegate)(object)dyn.CreateDelegate(delType);
}
public static TDelegate GetFieldSetter<TDelegate>(Type type, params string[] fields) where TDelegate : TDelegateBase
{
return GetFieldSetter<TDelegate>(fields.Select(f => type.GetField(f, privflags)).ToList());
}
public static TDelegate GetFieldSetter<TDelegate>(IList<FieldInfo> fields) where TDelegate : TDelegateBase
{
Type delType = typeof(TDelegate);
MethodSignature delSig = delType.GetDelegateSignature();
Type[] tParams = delSig.ParameterTypes;
Type retType = delSig.ReturnType;
DynamicMethod dyn = new DynamicMethod("set_"+String.Join(",", fields.Select(f => f.Name)), retType, tParams, typeof(Hacks).Module, true);
var il = dyn.GetILGenerator();
int add = 0;
if(fields.Any(f => !f.IsStatic))
{
add = 1;
}
for(int i = 0; i < fields.Count; i++)
{
var fi = fields[i];
if(fi.IsStatic)
{
il.EmitLdarg(i+add);
EmitCast(il, tParams[i+add], fi.FieldType);
il.Emit(OpCodes.Stsfld, fi);
}else{
EmitThis(il, tParams[0], fi.DeclaringType);
il.EmitLdarg(i+add);
EmitCast(il, tParams[i+add], fi.FieldType);
il.Emit(OpCodes.Stfld, fi);
}
}
il.Emit(OpCodes.Ret);
return (TDelegate)(object)dyn.CreateDelegate(delType);
}
public static TDelegate GetPropertyGetter<TDelegate>(Type type, string property) where TDelegate : TDelegateBase
{
return GetPropertyGetter<TDelegate>(type.GetProperty(property, privflags));
}
public static TDelegate GetPropertyGetter<TDelegate>(PropertyInfo pi) where TDelegate : TDelegateBase
{
return GetInvoker<TDelegate>(pi.GetGetMethod(true));
}
public static TDelegate GetPropertySetter<TDelegate>(Type type, string property) where TDelegate : TDelegateBase
{
return GetPropertySetter<TDelegate>(type.GetProperty(property, privflags));
}
public static TDelegate GetPropertySetter<TDelegate>(PropertyInfo pi) where TDelegate : TDelegateBase
{
return GetInvoker<TDelegate>(pi.GetSetMethod(true));
}
private static void EmitThis(ILGenerator il, Type argType, Type privateType)
{
il.Emit(OpCodes.Ldarg_0);
if(argType.IsByRef || argType.IsPointer)
{
argType = argType.GetElementType();
if(argType == privateType)
{
if(argType.IsValueType)
{
return;
}else{
il.Emit(OpCodes.Ldobj, argType);
}
}else if(argType.IsValueType)
{
il.Emit(OpCodes.Ldobj, argType);
}
}
EmitCast(il, argType, privateType);
}
private static void EmitCast(ILGenerator il, Type from, Type to)
{
if(from.IsValueType && !to.IsValueType)
{
il.Emit(OpCodes.Box, from);
}else if(!from.IsValueType && to.IsValueType)
{
il.Emit(OpCodes.Unbox, to);
return;
}
if(from.IsEnum && to == Enum.GetUnderlyingType(from) ||
to.IsEnum && from == Enum.GetUnderlyingType(to))
{
return;
}
if(TryEmitCast(from, to) || TryEmitCast(to, from))
{
il.Emit(OpCodes.Castclass, to);
}
}
private static bool TryEmitCast(Type a, Type b)
{
if(a == b)
{
return false;
}else if(a.IsAssignableFrom(b) || b.IsAssignableFrom(a))
{
return true;
}else if(a.IsEnum && Enum.GetUnderlyingType(a) == b)
{
return false;
}else if(a.IsEnum && b.IsEnum && Enum.GetUnderlyingType(a) == Enum.GetUnderlyingType(b))
{
return false;
}else if(a.IsPointer && b.IsPointer)
{
return false;
}else if(a.IsPointer && (b == typeof(IntPtr) || b == typeof(UIntPtr)))
{
return false;
}
return true;
}
}
}