forked from shmuelie/System.AppDomain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflectionHelpers.cs
240 lines (215 loc) · 11.8 KB
/
ReflectionHelpers.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
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace System
{
internal static class ReflectionHelpers
{
public static void GetEventMethods(this Type @this, string eventName, out Action<object, Delegate> addMethod, out Action<object, Delegate> removeMethod)
{
EventInfo eventInfo = @this.GetEvent(eventName, BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (eventInfo == null)
{
FieldInfo fieldInfo = @this.GetField($"_{char.ToLowerInvariant(eventName[0])}{eventName.Substring(1)}", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (fieldInfo == null)
{
addMethod = null;
removeMethod = null;
return;
}
EventFieldClosure efc = new EventFieldClosure(fieldInfo);
addMethod = efc.Add;
removeMethod = efc.Remove;
}
else
{
addMethod = CreateEventMethod(eventInfo.AddMethod);
removeMethod = CreateEventMethod(eventInfo.RemoveMethod);
}
}
private sealed class EventFieldClosure
{
private readonly FieldInfo fieldInfo;
public EventFieldClosure(FieldInfo fieldInfo)
{
this.fieldInfo = fieldInfo;
Add = new Action<object, Delegate>(AddMethod);
Remove = new Action<object, Delegate>(RemoveMethod);
}
private void AddMethod(object @this, Delegate value)
{
fieldInfo.SetValue(@this, Delegate.Combine((Delegate)fieldInfo.GetValue(@this), value));
}
private void RemoveMethod(object @this, Delegate value)
{
fieldInfo.SetValue(@this, Delegate.Remove((Delegate)fieldInfo.GetValue(@this), value));
}
public Action<object, Delegate> Add
{
get;
}
public Action<object, Delegate> Remove
{
get;
}
}
private static Action<object, Delegate> CreateEventMethod(MethodInfo methodInfo)
{
ParameterExpression thisParameter = Expression.Parameter(typeof(object));
UnaryExpression convertThis = Expression.Convert(thisParameter, methodInfo.DeclaringType);
ParameterExpression valueParamater = Expression.Parameter(typeof(Delegate));
UnaryExpression convertValue = Expression.Convert(valueParamater, methodInfo.GetParameters()[0].ParameterType);
return Expression.Lambda<Action<object, Delegate>>(Expression.Call(convertThis, methodInfo, Enumerable.Repeat(convertValue, 1)), false, thisParameter.Collect().AndThis(valueParamater)).Compile();
}
public static Delegate CreateEventDelegate<TEventArgs, TReturn>(this object @this, string onMethodName, Type realEventArgsType, Type realHandlerType) where TEventArgs : EventArgs
{
ParameterExpression eParameter = Expression.Parameter(typeof(TEventArgs));
ParameterExpression argsParameter = Expression.Parameter(realEventArgsType);
return Expression.Lambda(
realHandlerType,
Expression.Invoke(
Expression.Lambda<Func<TEventArgs, TReturn>>(
Expression.Call(
Expression.Constant(
@this, typeof(AppDomain)),
typeof(AppDomain).GetMethod(
onMethodName,
BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic),
Enumerable.Repeat(eParameter, 1)),
Enumerable.Repeat(
eParameter,
1)),
Enumerable.Repeat(
Expression.New(
eParameter.Type.GetConstructor(typeof(object)),
Enumerable.Repeat(
argsParameter,
1)),
1)),
false,
Expression.Parameter(typeof(object)).Collect().AndThis(argsParameter)).Compile();
}
public static Delegate CreateEventDelegate<TEventArgs>(this object @this, string onMethodName, Type realEventArgsType, Type realHandlerType) where TEventArgs : EventArgs
{
ParameterExpression eParameter = Expression.Parameter(typeof(TEventArgs));
ParameterExpression argsParameter = Expression.Parameter(realEventArgsType);
ConstructorInfo constructorInfo = eParameter.Type.GetConstructor(typeof(object)) ?? eParameter.Type.GetDefaultConstructor();
return Expression.Lambda(
realHandlerType,
Expression.Invoke(
Expression.Lambda<Action<TEventArgs>>(
Expression.Call(
Expression.Constant(
@this, typeof(AppDomain)),
typeof(AppDomain).GetMethod(
onMethodName,
BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic),
Enumerable.Repeat(eParameter, 1)),
Enumerable.Repeat(
eParameter,
1)),
Enumerable.Repeat(
Expression.New(
constructorInfo,
Enumerable.Repeat(
argsParameter,
constructorInfo.GetParameters().Length)),
1)),
false,
Expression.Parameter(typeof(object)).Collect().AndThis(argsParameter)).Compile();
}
public static void AttachOrDetachEvent(this object @this, MulticastDelegate @delegate, Delegate realDelegate, Action<object, Delegate> realAction)
{
if (@delegate == null || @delegate.GetInvocationList().Length == 0)
{
realAction?.Invoke(@this, realDelegate);
}
}
public static Func<object> GetStaticPropertyFunction(this Type @this, string propertyName) => Expression.Lambda<Func<object>>(Expression.Property(null, @this.GetProperty(propertyName, BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic).GetMethod), Enumerable.Repeat<ParameterExpression>(null, 0)).Compile();
public static Func<object, T> GetInstancePropertyFunction<T>(this Type @this, string propertyName)
{
PropertyInfo propertyInfo = @this.GetProperty(propertyName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (propertyInfo == null)
{
return null;
}
ParameterExpression parameter = Expression.Parameter(typeof(object));
return Expression.Lambda<Func<object, T>>(Expression.Property(Expression.Convert(parameter, @this), propertyInfo.GetMethod), true, Enumerable.Repeat(parameter, 1)).Compile();
}
public static Func<object, T> GetInstanceFieldFunction<T>(this Type @this, string fieldName)
{
ParameterExpression parameter = Expression.Parameter(typeof(object));
return Expression.Lambda<Func<object, T>>(Expression.Field(Expression.Convert(parameter, @this), @this.GetField(fieldName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)), true, Enumerable.Repeat(parameter, 1)).Compile();
}
public static Func<object, T> GetInstanceFunctionFunction<T>(this Type @this, string functionName)
{
ParameterExpression parameter = Expression.Parameter(typeof(object));
MethodInfo methodInfo = @this.GetMethod(functionName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (methodInfo == null)
{
return null;
}
return Expression.Lambda<Func<object, T>>(Expression.Call(Expression.Convert(parameter, @this), methodInfo), true, Enumerable.Repeat(parameter, 1)).Compile();
}
public static Func<object, TArg, TResult> GetInstanceFunctionFunction<TArg, TResult>(this Type @this, string functionName)
{
ParameterExpression thisParameter = Expression.Parameter(typeof(object));
ParameterExpression argParameter = Expression.Parameter(typeof(TArg));
MethodInfo[] methodInfos = @this.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
MethodInfo methodInfo = null;
for (int i = 0; i < methodInfos.Length; i++, methodInfo = null)
{
methodInfo = methodInfos[i];
if (methodInfo.Name == functionName)
{
ParameterInfo[] parameterInfos = methodInfo.GetParameters();
if (parameterInfos.Length == 1 && parameterInfos[0].ParameterType.IsSameAs(typeof(TArg)))
{
break;
}
}
}
if (methodInfo == null)
{
return null;
}
return Expression.Lambda<Func<object, TArg, TResult>>(Expression.Call(Expression.Convert(thisParameter, @this), methodInfo, Enumerable.Repeat(argParameter, 1)), true, thisParameter.Collect().AndThis(argParameter)).Compile();
}
public static ConstructorInfo GetDefaultConstructor(this Type @this) => @this.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(ci => ci.GetParameters().Length == 0);
public static ConstructorInfo GetConstructor(this Type @this, Type argumentType) => @this.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(new ConstructorArgumentFilter(argumentType).Predicate);
public static Func<TArg, object> CreateConstructor<TArg>(this Type @this)
{
ConstructorInfo constructorInfo = @this.GetConstructor(typeof(TArg));
if (constructorInfo == null)
{
return null;
}
ParameterExpression arg = Expression.Parameter(typeof(TArg));
return Expression.Lambda<Func<TArg, object>>(Expression.New(constructorInfo, arg.Collect()), true, arg.Collect()).Compile();
}
public static bool IsSameAs(this Type @this, Type other) => @this.Equals(other) || (@this.IsAssignableFrom(other) && other.IsAssignableFrom(@this));
private sealed class ConstructorArgumentFilter
{
private readonly Type argumentType;
public ConstructorArgumentFilter(Type argumentType)
{
this.argumentType = argumentType;
Predicate = new Func<ConstructorInfo, bool>(Filter);
}
private bool Filter(ConstructorInfo constructorInfo)
{
ParameterInfo[] parameterInfos = constructorInfo.GetParameters();
if (parameterInfos.Length != 1)
{
return false;
}
return argumentType.IsSameAs(parameterInfos[0].ParameterType);
}
public Func<ConstructorInfo, bool> Predicate
{
get;
}
}
public static Type RealType(this Type @this) => typeof(string).GetTypeInfo().Assembly.GetType(@this.FullName);
}
}