forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionExtensions.cs
More file actions
216 lines (187 loc) · 5.62 KB
/
ExpressionExtensions.cs
File metadata and controls
216 lines (187 loc) · 5.62 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
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Blog.Core.Common.Caches;
namespace Blog.Core.Common.Helper
{
/// <summary>
/// Linq扩展
/// </summary>
public static class ExpressionExtensions
{
#region HttpContext
/// <summary>
/// 返回请求上下文
/// </summary>
/// <param name="context"></param>
/// <param name="code"></param>
/// <param name="message"></param>
/// <param name="ContentType"></param>
/// <returns></returns>
public static async Task Cof_SendResponse(this HttpContext context, System.Net.HttpStatusCode code, string message,
string ContentType = "text/html;charset=utf-8")
{
context.Response.StatusCode = (int) code;
context.Response.ContentType = ContentType;
await context.Response.WriteAsync(message);
}
#endregion
#region ICaching
/// <summary>
/// 从缓存里取数据,如果不存在则执行查询方法,
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="cache">ICaching </param>
/// <param name="key">键值</param>
/// <param name="GetFun">查询方法</param>
/// <param name="timeSpanMin">有效期 单位分钟/param>
/// <returns></returns>
public static T Cof_GetICaching<T>(this ICaching cache, string key, Func<T> GetFun, int timeSpanMin) where T : class
{
var obj = cache.Get<T>(key);
if (obj == null)
{
obj = GetFun();
cache.Set(key, obj, TimeSpan.FromMinutes(timeSpanMin));
}
return obj;
}
/// <summary>
/// 异步从缓存里取数据,如果不存在则执行查询方法
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="cache">ICaching </param>
/// <param name="key">键值</param>
/// <param name="GetFun">查询方法</param>
/// <param name="timeSpanMin">有效期 单位分钟/param>
/// <returns></returns>
public static async Task<T> Cof_AsyncGetICaching<T>(this ICaching cache, string key, Func<Task<T>> GetFun, int timeSpanMin) where T : class
{
var obj = await cache.GetAsync<T>(key);
if (obj == null)
{
obj = await GetFun();
cache.Set(key, obj, TimeSpan.FromMinutes(timeSpanMin));
}
return obj;
}
#endregion
#region 常用扩展方法
public static bool Cof_CheckAvailable<TSource>(this IEnumerable<TSource> Tlist)
{
return Tlist != null && Tlist.Count() > 0;
}
/// <summary>
/// 调用内部方法
/// </summary>
public static Expression Call(this Expression instance, string methodName, params Expression[] arguments)
{
if (instance.Type == typeof(string))
return Expression.Call(instance, instance.Type.GetMethod(methodName, new Type[] {typeof(string)}),
arguments); //修复string contains 出现的问题 Ambiguous match found.
else
return Expression.Call(instance, instance.Type.GetMethod(methodName), arguments);
}
/// <summary>
/// 获取内部成员
/// </summary>
public static Expression Property(this Expression expression, string propertyName)
{
// Todo:左边条件如果是dynamic,
// 则Expression.Property无法获取子内容
// 报错在这里,由于expression内的对象为Object,所以无法解析到
// var x = (expression as IQueryable).ElementType;
var exp = Expression.Property(expression, propertyName);
if (exp.Type.IsGenericType && exp.Type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return Expression.Convert(exp, exp.Type.GetGenericArguments()[0]);
}
return exp;
}
/// <summary>
/// 转Lambda
/// </summary>
public static Expression<TDelegate> ToLambda<TDelegate>(this Expression body,
params ParameterExpression[] parameters)
{
return Expression.Lambda<TDelegate>(body, parameters);
}
#endregion
#region 常用运算符 [ > , >= , == , < , <= , != , || , && ]
/// <summary>
/// &&
/// </summary>
public static Expression AndAlso(this Expression left, Expression right)
{
return Expression.AndAlso(left, right);
}
/// <summary>
/// ||
/// </summary>
public static Expression OrElse(this Expression left, Expression right)
{
return Expression.OrElse(left, right);
}
/// <summary>
/// Contains
/// </summary>
public static Expression Contains(this Expression left, Expression right)
{
return left.Call("Contains", right);
}
public static Expression StartContains(this Expression left, Expression right)
{
return left.Call("StartsWith", right);
}
public static Expression EndContains(this Expression left, Expression right)
{
return left.Call("EndsWith", right);
}
/// <summary>
/// >
/// </summary>
public static Expression GreaterThan(this Expression left, Expression right)
{
return Expression.GreaterThan(left, right);
}
/// <summary>
/// >=
/// </summary>
public static Expression GreaterThanOrEqual(this Expression left, Expression right)
{
return Expression.GreaterThanOrEqual(left, right);
}
/// <summary>
/// <
/// </summary>
public static Expression LessThan(this Expression left, Expression right)
{
return Expression.LessThan(left, right);
}
/// <summary>
/// <=
/// </summary>
public static Expression LessThanOrEqual(this Expression left, Expression right)
{
return Expression.LessThanOrEqual(left, right);
}
/// <summary>
/// ==
/// </summary>
public static Expression Equal(this Expression left, Expression right)
{
return Expression.Equal(left, right);
}
/// <summary>
/// !=
/// </summary>
public static Expression NotEqual(this Expression left, Expression right)
{
return Expression.NotEqual(left, right);
}
#endregion
}
}