using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; namespace EntrustSettle.Common.Extensions { public static partial class Extention { #region 拓展BuildExtendSelectExpre方法 /// /// 组合继承属性选择表达式树,无拓展参数 /// TResult将继承TBase的所有属性 /// /// 原数据类型 /// 返回类型 /// 拓展表达式 /// public static Expression> BuildExtendSelectExpre(this Expression> expression) where TResult : TBase { return GetExtendSelectExpre>(expression); } /// /// 组合继承属性选择表达式树,1个拓展参数 /// TResult将继承TBase的所有属性 /// /// 原数据类型 /// 拓展类型1 /// 返回类型 /// 拓展表达式 /// public static Expression> BuildExtendSelectExpre(this Expression> expression) where TResult : TBase { return GetExtendSelectExpre>(expression); } /// /// 组合继承属性选择表达式树,2个拓展参数 /// TResult将继承TBase的所有属性 /// /// 原数据类型 /// 拓展类型1 /// 拓展类型2 /// 返回类型 /// 拓展表达式 /// public static Expression> BuildExtendSelectExpre(this Expression> expression) where TResult : TBase { return GetExtendSelectExpre>(expression); } /// /// 组合继承属性选择表达式树,3个拓展参数 /// TResult将继承TBase的所有属性 /// /// 原数据类型 /// 拓展类型1 /// 拓展类型2 /// 拓展类型3 /// 返回类型 /// 拓展表达式 /// public static Expression> BuildExtendSelectExpre(this Expression> expression) where TResult : TBase { return GetExtendSelectExpre>(expression); } /// /// 组合继承属性选择表达式树,4个拓展参数 /// TResult将继承TBase的所有属性 /// /// 原数据类型 /// 拓展类型1 /// 拓展类型2 /// 拓展类型3 /// 拓展类型4 /// 返回类型 /// 拓展表达式 /// public static Expression> BuildExtendSelectExpre(this Expression> expression) where TResult : TBase { return GetExtendSelectExpre>(expression); } /// /// 组合继承属性选择表达式树,5个拓展参数 /// TResult将继承TBase的所有属性 /// /// 原数据类型 /// 拓展类型1 /// 拓展类型2 /// 拓展类型3 /// 拓展类型4 /// 拓展类型5 /// 返回类型 /// 拓展表达式 /// public static Expression> BuildExtendSelectExpre(this Expression> expression) where TResult : TBase { return GetExtendSelectExpre>(expression); } /// /// 组合继承属性选择表达式树,6个拓展参数 /// TResult将继承TBase的所有属性 /// /// 原数据类型 /// 拓展类型1 /// 拓展类型2 /// 拓展类型3 /// 拓展类型4 /// 拓展类型5 /// 拓展类型6 /// 返回类型 /// 拓展表达式 /// public static Expression> BuildExtendSelectExpre(this Expression> expression) where TResult : TBase { return GetExtendSelectExpre>(expression); } /// /// 组合继承属性选择表达式树,7个拓展参数 /// TResult将继承TBase的所有属性 /// /// 原数据类型 /// 拓展类型1 /// 拓展类型2 /// 拓展类型3 /// 拓展类型4 /// 拓展类型5 /// 拓展类型6 /// 拓展类型7 /// 返回类型 /// 拓展表达式 /// public static Expression> BuildExtendSelectExpre(this Expression> expression) where TResult : TBase { return GetExtendSelectExpre>(expression); } /// /// 组合继承属性选择表达式树,8个拓展参数 /// TResult将继承TBase的所有属性 /// /// 原数据类型 /// 拓展类型1 /// 拓展类型2 /// 拓展类型3 /// 拓展类型4 /// 拓展类型5 /// 拓展类型6 /// 拓展类型7 /// 拓展类型8 /// 返回类型 /// 拓展表达式 /// public static Expression> BuildExtendSelectExpre(this Expression> expression) where TResult : TBase { return GetExtendSelectExpre>(expression); } /// /// 组合继承属性选择表达式树,9个拓展参数 /// TResult将继承TBase的所有属性 /// /// 原数据类型 /// 拓展类型1 /// 拓展类型2 /// 拓展类型3 /// 拓展类型4 /// 拓展类型5 /// 拓展类型6 /// 拓展类型7 /// 拓展类型8 /// 拓展类型9 /// 返回类型 /// 拓展表达式 /// public static Expression> BuildExtendSelectExpre(this Expression> expression) where TResult : TBase { return GetExtendSelectExpre>(expression); } #endregion #region 拓展And和Or方法 /// /// 连接表达式与运算 /// /// 参数 /// 原表达式 /// 新的表达式 /// public static Expression> And(this Expression> one, Expression> 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.AndAlso(left, right); return Expression.Lambda>(body, newParameter); } /// /// 连接表达式或运算 /// /// 参数 /// 原表达式 /// 新表达式 /// public static Expression> Or(this Expression> one, Expression> 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>(body, newParameter); } #endregion #region 拓展Expression的Invoke方法 public static TResult Invoke(this Expression> expression) { return expression.Compile().Invoke(); } public static TResult Invoke(this Expression> expression, T1 arg1) { return expression.Compile().Invoke(arg1); } public static TResult Invoke(this Expression> expression, T1 arg1, T2 arg2) { return expression.Compile().Invoke(arg1, arg2); } public static TResult Invoke(this Expression> expression, T1 arg1, T2 arg2, T3 arg3) { return expression.Compile().Invoke(arg1, arg2, arg3); } public static TResult Invoke(this Expression> expression, T1 arg1, T2 arg2, T3 arg3, T4 arg4) { return expression.Compile().Invoke(arg1, arg2, arg3, arg4); } public static TResult Invoke(this Expression> expression, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) { return expression.Compile().Invoke(arg1, arg2, arg3, arg4, arg5); } public static TResult Invoke(this Expression> 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(this Expression> 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(this Expression> 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(this Expression> 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(this Expression> 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 /// /// 获取表达式中的固定值 /// /// 表达式 /// 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 GetExtendSelectExpre(Expression expression) { NewExpression newBody = Expression.New(typeof(TResult)); MemberInitExpression oldExpression = (MemberInitExpression)expression.Body; ParameterExpression[] oldParamters = expression.Parameters.ToArray(); List existsProperties = new List(); foreach (var aBinding in oldExpression.Bindings) { existsProperties.Add(aBinding.Member.Name); } List newBindings = new List(); 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(body, oldParamters); return resExpression; } #endregion } /// /// 继承ExpressionVisitor类,实现参数替换统一 /// 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; } } }