using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using EntrustSettle.Common.Caches;
namespace EntrustSettle.Common.Helper
{
///
/// Linq扩展
///
public static class ExpressionExtensions
{
#region HttpContext
///
/// 返回请求上下文
///
///
///
///
///
///
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
///
/// 从缓存里取数据,如果不存在则执行查询方法,
///
/// 类型
/// ICaching
/// 键值
/// 查询方法
/// 有效期 单位分钟/param>
///
public static T Cof_GetICaching(this ICaching cache, string key, Func GetFun, int timeSpanMin) where T : class
{
var obj = cache.Get(key);
if (obj == null)
{
obj = GetFun();
cache.Set(key, obj, TimeSpan.FromMinutes(timeSpanMin));
}
return obj;
}
///
/// 异步从缓存里取数据,如果不存在则执行查询方法
///
/// 类型
/// ICaching
/// 键值
/// 查询方法
/// 有效期 单位分钟/param>
///
public static async Task Cof_AsyncGetICaching(this ICaching cache, string key, Func> GetFun, int timeSpanMin) where T : class
{
var obj = await cache.GetAsync(key);
if (obj == null)
{
obj = await GetFun();
cache.Set(key, obj, TimeSpan.FromMinutes(timeSpanMin));
}
return obj;
}
#endregion
#region 常用扩展方法
public static bool Cof_CheckAvailable(this IEnumerable Tlist)
{
return Tlist != null && Tlist.Count() > 0;
}
///
/// 调用内部方法
///
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);
}
///
/// 获取内部成员
///
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;
}
///
/// 转Lambda
///
public static Expression ToLambda(this Expression body,
params ParameterExpression[] parameters)
{
return Expression.Lambda(body, parameters);
}
#endregion
#region 常用运算符 [ > , >= , == , < , <= , != , || , && ]
///
/// &&
///
public static Expression AndAlso(this Expression left, Expression right)
{
return Expression.AndAlso(left, right);
}
///
/// ||
///
public static Expression OrElse(this Expression left, Expression right)
{
return Expression.OrElse(left, right);
}
///
/// Contains
///
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);
}
///
/// >
///
public static Expression GreaterThan(this Expression left, Expression right)
{
return Expression.GreaterThan(left, right);
}
///
/// >=
///
public static Expression GreaterThanOrEqual(this Expression left, Expression right)
{
return Expression.GreaterThanOrEqual(left, right);
}
///
/// <
///
public static Expression LessThan(this Expression left, Expression right)
{
return Expression.LessThan(left, right);
}
///
/// <=
///
public static Expression LessThanOrEqual(this Expression left, Expression right)
{
return Expression.LessThanOrEqual(left, right);
}
///
/// ==
///
public static Expression Equal(this Expression left, Expression right)
{
return Expression.Equal(left, right);
}
///
/// !=
///
public static Expression NotEqual(this Expression left, Expression right)
{
return Expression.NotEqual(left, right);
}
#endregion
}
}