/
FluentAspectOptionsExtensions.cs
353 lines (289 loc) · 12.5 KB
/
FluentAspectOptionsExtensions.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Reflection;
using WeihanLi.Common.Helpers;
using WeihanLi.Extensions;
namespace WeihanLi.Common.Aspect;
public static class FluentAspectOptionsExtensions
{
#region Intercept
public static IInterceptionConfiguration Intercept(this FluentAspectOptions options,
Func<IInvocation, bool> predict)
{
Guard.NotNull(predict);
if (options.InterceptionConfigurations.TryGetValue
(predict, out var interceptionConfiguration))
{
return interceptionConfiguration;
}
interceptionConfiguration = new InterceptionConfiguration();
options.InterceptionConfigurations[predict] = interceptionConfiguration;
return interceptionConfiguration;
}
public static IInterceptionConfiguration InterceptAll(this FluentAspectOptions options)
=> options.Intercept(_ => true);
public static IInterceptionConfiguration InterceptType(this FluentAspectOptions options,
Func<Type, bool> typesFilter)
{
Guard.NotNull(typesFilter);
return options.InterceptMethod(m => typesFilter(m.DeclaringType!));
}
public static IInterceptionConfiguration InterceptMethod<T>(this FluentAspectOptions options,
Expression<Func<T, object>> method)
{
return options.InterceptMethod<T>(method.GetMethodExpression());
}
public static IInterceptionConfiguration InterceptMethod<T>(this FluentAspectOptions options,
MethodInfo method)
{
Guard.NotNull(method);
var methodSignature = method.GetSignature();
return options.InterceptMethod<T>(m => m.GetSignature().Equals(methodSignature));
}
public static IInterceptionConfiguration InterceptMethod(this FluentAspectOptions options,
MethodInfo method)
{
Guard.NotNull(method);
var methodSignature = method.GetSignature();
return options.InterceptMethod(m => m.GetSignature().Equals(methodSignature));
}
public static IInterceptionConfiguration InterceptPropertyGetter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(this FluentAspectOptions options,
Expression<Func<T, object>> expression)
{
var prop = expression.GetProperty();
if (null == prop)
{
throw new InvalidOperationException("no property found");
}
if (!prop.CanRead || prop.GetMethod == null)
{
throw new InvalidOperationException($"the property {prop.Name} can not read");
}
return options.InterceptMethod<T>(prop.GetMethod);
}
public static IInterceptionConfiguration InterceptPropertySetter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(this FluentAspectOptions options,
Expression<Func<T, object>> expression)
{
var prop = expression.GetProperty();
if (null == prop)
{
throw new InvalidOperationException("no property found");
}
if (!prop.CanWrite || prop.SetMethod == null)
{
throw new InvalidOperationException($"the property {prop.Name} can not write");
}
return options.InterceptMethod<T>(prop.SetMethod);
}
public static IInterceptionConfiguration InterceptMethod<T>(this FluentAspectOptions options,
Expression<Action<T>> method)
{
return options.InterceptMethod<T>(method.GetMethodExpression());
}
public static IInterceptionConfiguration InterceptType<T>(this FluentAspectOptions options)
{
return options.InterceptMethod(m => m.DeclaringType!.IsAssignableTo<T>());
}
public static IInterceptionConfiguration InterceptMethod<T>(this FluentAspectOptions options,
Expression<Func<MethodInfo, bool>> andExpression)
{
Expression<Func<MethodInfo, bool>> expression = m => m.DeclaringType!.IsAssignableTo<T>();
expression = expression.And(Guard.NotNull(andExpression, nameof(andExpression)));
return options.InterceptMethod(expression.Compile());
}
public static IInterceptionConfiguration InterceptMethod(this FluentAspectOptions options,
Func<MethodInfo, bool> methodPredict)
{
return options.Intercept(invocation => methodPredict(invocation.Method ?? invocation.ProxyMethod));
}
public static IInterceptionConfiguration InterceptMethod<T>(this FluentAspectOptions options,
MethodCallExpression methodCallExpression)
{
var innerMethod = methodCallExpression.Method;
if (null == innerMethod)
{
throw new InvalidOperationException("no method found");
}
Expression<Func<MethodInfo, bool>> expression = m => m.DeclaringType!.IsAssignableTo<T>();
var methodSignature = innerMethod.GetSignature();
expression = expression.And(m => m.GetSignature().Equals(methodSignature));
return options.InterceptMethod(expression.Compile());
}
#endregion Intercept
#region NoIntercept
public static bool NoIntercept(this FluentAspectOptions options, Func<IInvocation, bool> predict)
{
return options.NoInterceptionConfigurations.Add(predict);
}
public static FluentAspectOptions NoInterceptMethod<T>(this FluentAspectOptions options,
Expression<Func<T, object>> method)
{
options.NoInterceptMethod<T>(method.GetMethodExpression());
return options;
}
public static FluentAspectOptions NoInterceptMethod<T>(this FluentAspectOptions options,
Expression<Action<T>> method)
{
options.NoInterceptMethod<T>(method.GetMethodExpression());
return options;
}
public static FluentAspectOptions NoInterceptType(this FluentAspectOptions options,
Func<Type, bool> typesFilter)
{
Guard.NotNull(options, nameof(options));
Guard.NotNull(typesFilter, nameof(typesFilter));
options.NoInterceptMethod(m => typesFilter(m.DeclaringType!));
return options;
}
public static FluentAspectOptions NoInterceptType<T>(this FluentAspectOptions options)
{
options.NoInterceptMethod(m => m.DeclaringType!.IsAssignableTo<T>());
return options;
}
public static FluentAspectOptions NoInterceptMethod<T>(this FluentAspectOptions options,
Expression<Func<MethodInfo, bool>> andExpression)
{
Expression<Func<MethodInfo, bool>> expression = m => m.DeclaringType!.IsAssignableTo<T>();
expression = expression.And(Guard.NotNull(andExpression, nameof(andExpression)));
options.NoInterceptMethod(expression.Compile());
return options;
}
public static FluentAspectOptions NoInterceptMethod<T>(this FluentAspectOptions options,
MethodCallExpression methodCallExpression)
{
var innerMethod = methodCallExpression.Method;
if (null == innerMethod)
{
throw new InvalidOperationException($"no method found");
}
Expression<Func<MethodInfo, bool>> expression = m => m.DeclaringType!.IsAssignableTo<T>();
var methodSignature = innerMethod.GetSignature();
expression = expression.And(m => m.GetSignature().Equals(methodSignature));
return options.NoInterceptMethod(expression.Compile());
}
public static FluentAspectOptions NoInterceptMethod(this FluentAspectOptions options,
Func<MethodInfo, bool> methodPredict)
{
options.NoIntercept(invocation => methodPredict(invocation.Method ?? invocation.ProxyMethod));
return options;
}
public static FluentAspectOptions NoInterceptMethod<T>(this FluentAspectOptions options,
MethodInfo method)
{
Guard.NotNull(method);
var methodSignature = method.GetSignature();
return options.NoInterceptMethod<T>(m => m.GetSignature().Equals(methodSignature));
}
public static FluentAspectOptions NoInterceptMethod(this FluentAspectOptions options,
MethodInfo method)
{
Guard.NotNull(method);
var methodSignature = method.GetSignature();
return options.NoInterceptMethod(m => m.GetSignature().Equals(methodSignature));
}
public static FluentAspectOptions NoInterceptProperty<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(this FluentAspectOptions options,
Expression<Func<T, object>> expression)
{
var prop = expression.GetProperty();
if (null == prop)
{
throw new InvalidOperationException("no property found");
}
if (prop.GetMethod != null)
{
options = options.NoInterceptMethod<T>(prop.GetMethod);
}
if (prop.SetMethod != null)
{
options = options.NoInterceptMethod<T>(prop.SetMethod);
}
return options;
}
public static FluentAspectOptions NoInterceptPropertyGetter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(this FluentAspectOptions options,
Expression<Func<T, object>> expression)
{
var prop = expression.GetProperty();
if (null == prop)
{
throw new InvalidOperationException("no property found");
}
if (!prop.CanRead || prop.GetMethod == null)
{
throw new InvalidOperationException($"the property {prop.Name} can not read");
}
return options.NoInterceptMethod<T>(prop.GetMethod);
}
public static FluentAspectOptions NoInterceptPropertySetter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(this FluentAspectOptions options,
Expression<Func<T, object>> expression)
{
var prop = expression.GetProperty();
if (null == prop)
{
throw new InvalidOperationException("no property found");
}
if (!prop.CanWrite || prop.SetMethod == null)
{
throw new InvalidOperationException($"the property {prop.Name} can not write");
}
return options.NoInterceptMethod<T>(prop.SetMethod);
}
#endregion NoIntercept
#region InterceptorResolver
public static FluentAspectOptions UseInterceptorResolver(this FluentAspectOptions options,
IInterceptorResolver resolver)
{
Guard.NotNull(resolver);
options.InterceptorResolver = resolver;
return options;
}
public static FluentAspectOptions UseInterceptorResolver<TResolver>(this FluentAspectOptions options)
where TResolver : IInterceptorResolver, new()
{
options.InterceptorResolver = new TResolver();
return options;
}
#endregion InterceptorResolver
#region ProxyFactory
public static FluentAspectOptions UseProxyFactory(this FluentAspectOptions options, IProxyFactory proxyFactory)
{
options.ProxyFactory = proxyFactory;
return options;
}
public static FluentAspectOptions UseProxyFactory<TProxyFactory>(this FluentAspectOptions options)
where TProxyFactory : class, IProxyFactory, new()
{
options.ProxyFactory = new TProxyFactory();
return options;
}
public static FluentAspectOptions UseProxyFactory<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TProxyFactory>(this FluentAspectOptions options,
params object[] parameters) where TProxyFactory : class, IProxyFactory
{
options.ProxyFactory = ActivatorHelper.CreateInstance<TProxyFactory>(parameters);
return options;
}
#endregion ProxyFactory
#region Enricher
public static FluentAspectOptions WithProperty(this FluentAspectOptions options, string propertyName,
object propertyValue, bool overwrite = false)
{
options.Enrichers.Add(new PropertyInvocationEnricher(propertyName, propertyValue, overwrite));
return options;
}
public static FluentAspectOptions WithProperty(this FluentAspectOptions options, string propertyName,
Func<IInvocation, object> propertyValueFactory, bool overwrite = false)
{
options.Enrichers.Add(new PropertyInvocationEnricher(propertyName, propertyValueFactory, overwrite));
return options;
}
public static FluentAspectOptions WithEnricher<TEnricher>(this FluentAspectOptions options) where TEnricher : IInvocationEnricher, new()
{
options.Enrichers.Add(new TEnricher());
return options;
}
public static FluentAspectOptions WithEnricher<TEnricher>(this FluentAspectOptions options, TEnricher enricher) where TEnricher : IInvocationEnricher
{
options.Enrichers.Add(enricher);
return options;
}
#endregion Enricher
}