forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtention.Expression.cs
More file actions
387 lines (337 loc) · 17.9 KB
/
Extention.Expression.cs
File metadata and controls
387 lines (337 loc) · 17.9 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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace Blog.Core.Common.Extensions
{
public static partial class Extention
{
#region 拓展BuildExtendSelectExpre方法
/// <summary>
/// 组合继承属性选择表达式树,无拓展参数
/// TResult将继承TBase的所有属性
/// </summary>
/// <typeparam name="TBase">原数据类型</typeparam>
/// <typeparam name="TResult">返回类型</typeparam>
/// <param name="expression">拓展表达式</param>
/// <returns></returns>
public static Expression<Func<TBase, TResult>> BuildExtendSelectExpre<TBase, TResult>(this Expression<Func<TBase, TResult>> expression) where TResult : TBase
{
return GetExtendSelectExpre<TBase, TResult, Func<TBase, TResult>>(expression);
}
/// <summary>
/// 组合继承属性选择表达式树,1个拓展参数
/// TResult将继承TBase的所有属性
/// </summary>
/// <typeparam name="TBase">原数据类型</typeparam>
/// <typeparam name="T1">拓展类型1</typeparam>
/// <typeparam name="TResult">返回类型</typeparam>
/// <param name="expression">拓展表达式</param>
/// <returns></returns>
public static Expression<Func<TBase, T1, TResult>> BuildExtendSelectExpre<TBase, T1, TResult>(this Expression<Func<TBase, T1, TResult>> expression) where TResult : TBase
{
return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, TResult>>(expression);
}
/// <summary>
/// 组合继承属性选择表达式树,2个拓展参数
/// TResult将继承TBase的所有属性
/// </summary>
/// <typeparam name="TBase">原数据类型</typeparam>
/// <typeparam name="T1">拓展类型1</typeparam>
/// <typeparam name="T2">拓展类型2</typeparam>
/// <typeparam name="TResult">返回类型</typeparam>
/// <param name="expression">拓展表达式</param>
/// <returns></returns>
public static Expression<Func<TBase, T1, T2, TResult>> BuildExtendSelectExpre<TBase, T1, T2, TResult>(this Expression<Func<TBase, T1, T2, TResult>> expression) where TResult : TBase
{
return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, T2, TResult>>(expression);
}
/// <summary>
/// 组合继承属性选择表达式树,3个拓展参数
/// TResult将继承TBase的所有属性
/// </summary>
/// <typeparam name="TBase">原数据类型</typeparam>
/// <typeparam name="T1">拓展类型1</typeparam>
/// <typeparam name="T2">拓展类型2</typeparam>
/// <typeparam name="T3">拓展类型3</typeparam>
/// <typeparam name="TResult">返回类型</typeparam>
/// <param name="expression">拓展表达式</param>
/// <returns></returns>
public static Expression<Func<TBase, T1, T2, T3, TResult>> BuildExtendSelectExpre<TBase, T1, T2, T3, TResult>(this Expression<Func<TBase, T1, T2, T3, TResult>> expression) where TResult : TBase
{
return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, T2, T3, TResult>>(expression);
}
/// <summary>
/// 组合继承属性选择表达式树,4个拓展参数
/// TResult将继承TBase的所有属性
/// </summary>
/// <typeparam name="TBase">原数据类型</typeparam>
/// <typeparam name="T1">拓展类型1</typeparam>
/// <typeparam name="T2">拓展类型2</typeparam>
/// <typeparam name="T3">拓展类型3</typeparam>
/// <typeparam name="T4">拓展类型4</typeparam>
/// <typeparam name="TResult">返回类型</typeparam>
/// <param name="expression">拓展表达式</param>
/// <returns></returns>
public static Expression<Func<TBase, T1, T2, T3, T4, TResult>> BuildExtendSelectExpre<TBase, T1, T2, T3, T4, TResult>(this Expression<Func<TBase, T1, T2, T3, T4, TResult>> expression) where TResult : TBase
{
return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, T2, T3, T4, TResult>>(expression);
}
/// <summary>
/// 组合继承属性选择表达式树,5个拓展参数
/// TResult将继承TBase的所有属性
/// </summary>
/// <typeparam name="TBase">原数据类型</typeparam>
/// <typeparam name="T1">拓展类型1</typeparam>
/// <typeparam name="T2">拓展类型2</typeparam>
/// <typeparam name="T3">拓展类型3</typeparam>
/// <typeparam name="T4">拓展类型4</typeparam>
/// <typeparam name="T5">拓展类型5</typeparam>
/// <typeparam name="TResult">返回类型</typeparam>
/// <param name="expression">拓展表达式</param>
/// <returns></returns>
public static Expression<Func<TBase, T1, T2, T3, T4, T5, TResult>> BuildExtendSelectExpre<TBase, T1, T2, T3, T4, T5, TResult>(this Expression<Func<TBase, T1, T2, T3, T4, T5, TResult>> expression) where TResult : TBase
{
return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, T2, T3, T4, T5, TResult>>(expression);
}
/// <summary>
/// 组合继承属性选择表达式树,6个拓展参数
/// TResult将继承TBase的所有属性
/// </summary>
/// <typeparam name="TBase">原数据类型</typeparam>
/// <typeparam name="T1">拓展类型1</typeparam>
/// <typeparam name="T2">拓展类型2</typeparam>
/// <typeparam name="T3">拓展类型3</typeparam>
/// <typeparam name="T4">拓展类型4</typeparam>
/// <typeparam name="T5">拓展类型5</typeparam>
/// <typeparam name="T6">拓展类型6</typeparam>
/// <typeparam name="TResult">返回类型</typeparam>
/// <param name="expression">拓展表达式</param>
/// <returns></returns>
public static Expression<Func<TBase, T1, T2, T3, T4, T5, T6, TResult>> BuildExtendSelectExpre<TBase, T1, T2, T3, T4, T5, T6, TResult>(this Expression<Func<TBase, T1, T2, T3, T4, T5, T6, TResult>> expression) where TResult : TBase
{
return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, T2, T3, T4, T5, T6, TResult>>(expression);
}
/// <summary>
/// 组合继承属性选择表达式树,7个拓展参数
/// TResult将继承TBase的所有属性
/// </summary>
/// <typeparam name="TBase">原数据类型</typeparam>
/// <typeparam name="T1">拓展类型1</typeparam>
/// <typeparam name="T2">拓展类型2</typeparam>
/// <typeparam name="T3">拓展类型3</typeparam>
/// <typeparam name="T4">拓展类型4</typeparam>
/// <typeparam name="T5">拓展类型5</typeparam>
/// <typeparam name="T6">拓展类型6</typeparam>
/// <typeparam name="T7">拓展类型7</typeparam>
/// <typeparam name="TResult">返回类型</typeparam>
/// <param name="expression">拓展表达式</param>
/// <returns></returns>
public static Expression<Func<TBase, T1, T2, T3, T4, T5, T6, T7, TResult>> BuildExtendSelectExpre<TBase, T1, T2, T3, T4, T5, T6, T7, TResult>(this Expression<Func<TBase, T1, T2, T3, T4, T5, T6, T7, TResult>> expression) where TResult : TBase
{
return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, T2, T3, T4, T5, T6, T7, TResult>>(expression);
}
/// <summary>
/// 组合继承属性选择表达式树,8个拓展参数
/// TResult将继承TBase的所有属性
/// </summary>
/// <typeparam name="TBase">原数据类型</typeparam>
/// <typeparam name="T1">拓展类型1</typeparam>
/// <typeparam name="T2">拓展类型2</typeparam>
/// <typeparam name="T3">拓展类型3</typeparam>
/// <typeparam name="T4">拓展类型4</typeparam>
/// <typeparam name="T5">拓展类型5</typeparam>
/// <typeparam name="T6">拓展类型6</typeparam>
/// <typeparam name="T7">拓展类型7</typeparam>
/// <typeparam name="T8">拓展类型8</typeparam>
/// <typeparam name="TResult">返回类型</typeparam>
/// <param name="expression">拓展表达式</param>
/// <returns></returns>
public static Expression<Func<TBase, T1, T2, T3, T4, T5, T6, T7, T8, TResult>> BuildExtendSelectExpre<TBase, T1, T2, T3, T4, T5, T6, T7, T8, TResult>(this Expression<Func<TBase, T1, T2, T3, T4, T5, T6, T7, T8, TResult>> expression) where TResult : TBase
{
return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, T2, T3, T4, T5, T6, T7, T8, TResult>>(expression);
}
/// <summary>
/// 组合继承属性选择表达式树,9个拓展参数
/// TResult将继承TBase的所有属性
/// </summary>
/// <typeparam name="TBase">原数据类型</typeparam>
/// <typeparam name="T1">拓展类型1</typeparam>
/// <typeparam name="T2">拓展类型2</typeparam>
/// <typeparam name="T3">拓展类型3</typeparam>
/// <typeparam name="T4">拓展类型4</typeparam>
/// <typeparam name="T5">拓展类型5</typeparam>
/// <typeparam name="T6">拓展类型6</typeparam>
/// <typeparam name="T7">拓展类型7</typeparam>
/// <typeparam name="T8">拓展类型8</typeparam>
/// <typeparam name="T9">拓展类型9</typeparam>
/// <typeparam name="TResult">返回类型</typeparam>
/// <param name="expression">拓展表达式</param>
/// <returns></returns>
public static Expression<Func<TBase, T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>> BuildExtendSelectExpre<TBase, T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>(this Expression<Func<TBase, T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>> expression) where TResult : TBase
{
return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>>(expression);
}
#endregion
#region 拓展And和Or方法
/// <summary>
/// 连接表达式与运算
/// </summary>
/// <typeparam name="T">参数</typeparam>
/// <param name="one">原表达式</param>
/// <param name="another">新的表达式</param>
/// <returns></returns>
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> one, Expression<Func<T, bool>> another)
{
//创建新参数
var newParameter = Expression.Parameter(typeof(T), "parameter");
var parameterReplacer = new ParameterReplaceVisitor(newParameter);
var left = parameterReplacer.Visit(one.Body);
var right = parameterReplacer.Visit(another.Body);
var body = Expression.And(left, right);
return Expression.Lambda<Func<T, bool>>(body, newParameter);
}
/// <summary>
/// 连接表达式或运算
/// </summary>
/// <typeparam name="T">参数</typeparam>
/// <param name="one">原表达式</param>
/// <param name="another">新表达式</param>
/// <returns></returns>
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> one, Expression<Func<T, bool>> another)
{
//创建新参数
var newParameter = Expression.Parameter(typeof(T), "parameter");
var parameterReplacer = new ParameterReplaceVisitor(newParameter);
var left = parameterReplacer.Visit(one.Body);
var right = parameterReplacer.Visit(another.Body);
var body = Expression.Or(left, right);
return Expression.Lambda<Func<T, bool>>(body, newParameter);
}
#endregion
#region 拓展Expression的Invoke方法
public static TResult Invoke<TResult>(this Expression<Func<TResult>> expression)
{
return expression.Compile().Invoke();
}
public static TResult Invoke<T1, TResult>(this Expression<Func<T1, TResult>> expression, T1 arg1)
{
return expression.Compile().Invoke(arg1);
}
public static TResult Invoke<T1, T2, TResult>(this Expression<Func<T1, T2, TResult>> expression, T1 arg1, T2 arg2)
{
return expression.Compile().Invoke(arg1, arg2);
}
public static TResult Invoke<T1, T2, T3, TResult>(this Expression<Func<T1, T2, T3, TResult>> expression, T1 arg1, T2 arg2, T3 arg3)
{
return expression.Compile().Invoke(arg1, arg2, arg3);
}
public static TResult Invoke<T1, T2, T3, T4, TResult>(this Expression<Func<T1, T2, T3, T4, TResult>> expression, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
return expression.Compile().Invoke(arg1, arg2, arg3, arg4);
}
public static TResult Invoke<T1, T2, T3, T4, T5, TResult>(this Expression<Func<T1, T2, T3, T4, T5, TResult>> expression, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
{
return expression.Compile().Invoke(arg1, arg2, arg3, arg4, arg5);
}
public static TResult Invoke<T1, T2, T3, T4, T5, T6, TResult>(this Expression<Func<T1, T2, T3, T4, T5, T6, TResult>> expression, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
{
return expression.Compile().Invoke(arg1, arg2, arg3, arg4, arg5, arg6);
}
public static TResult Invoke<T1, T2, T3, T4, T5, T6, T7, TResult>(this Expression<Func<T1, T2, T3, T4, T5, T6, T7, TResult>> expression, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7)
{
return expression.Compile().Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
}
public static TResult Invoke<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(this Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult>> expression, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8)
{
return expression.Compile().Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
}
public static TResult Invoke<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>(this Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>> expression, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9)
{
return expression.Compile().Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
}
public static TResult Invoke<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>(this Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>> expression, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10)
{
return expression.Compile().Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
}
#endregion
/// <summary>
/// 获取表达式中的固定值
/// </summary>
/// <param name="expression">表达式</param>
/// <returns></returns>
public static object GetConstantValue(this Expression expression)
{
var visitor = new GetConstantValueVisitor();
visitor.Visit(expression);
return visitor.ConstantValue;
}
public static object GetMemberValue(this Expression expression)
{
var visitor = new GetMemberValueVisitor();
visitor.Visit(expression);
return visitor.Value;
}
#region 私有成员
private static Expression<TDelegate> GetExtendSelectExpre<TBase, TResult, TDelegate>(Expression<TDelegate> expression)
{
NewExpression newBody = Expression.New(typeof(TResult));
MemberInitExpression oldExpression = (MemberInitExpression)expression.Body;
ParameterExpression[] oldParamters = expression.Parameters.ToArray();
List<string> existsProperties = new List<string>();
foreach (var aBinding in oldExpression.Bindings)
{
existsProperties.Add(aBinding.Member.Name);
}
List<MemberBinding> newBindings = new List<MemberBinding>();
var ls = typeof(TResult).GetProperties().Where(x => !existsProperties.Contains(x.Name));
foreach (var aProperty in ls)
{
if (typeof(TBase).GetMembers().Any(x => x.Name == aProperty.Name))
{
MemberInfo newMember = typeof(TBase).GetMember(aProperty.Name)[0];
MemberBinding newMemberBinding = Expression.Bind(newMember, Expression.Property(oldParamters[0], aProperty.Name));
newBindings.Add(newMemberBinding);
}
}
newBindings.AddRange(oldExpression.Bindings);
var body = Expression.MemberInit(newBody, newBindings.ToArray());
var resExpression = Expression.Lambda<TDelegate>(body, oldParamters);
return resExpression;
}
#endregion
}
/// <summary>
/// 继承ExpressionVisitor类,实现参数替换统一
/// </summary>
class ParameterReplaceVisitor : ExpressionVisitor
{
public ParameterReplaceVisitor(ParameterExpression paramExpr)
{
_parameter = paramExpr;
}
//新的表达式参数
private ParameterExpression _parameter { get; set; }
protected override Expression VisitParameter(ParameterExpression p)
{
if (p.Type == _parameter.Type)
return _parameter;
else
return p;
}
}
class GetConstantValueVisitor : ExpressionVisitor
{
public object ConstantValue { get; set; }
protected override Expression VisitConstant(ConstantExpression node)
{
ConstantValue = node.Value;
return base.VisitConstant(node);
}
}
class GetMemberValueVisitor : ExpressionVisitor
{
public object Value { get; set; }
}
}