(精华)2020年6月26日 C#类库 Expression(扩展方法)

using LinqKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Core.Util
{
    public static partial class Extention
    {
        #region 拓展AndIf与AndOr

        /// 
        /// 符合条件则And
        /// 
        /// 实体类型
        /// 原表达式
        /// 是否符合条件
        /// 新表达式
        /// 
        public static Expression<Func<T, bool>> AndIf<T>(this Expression<Func<T, bool>> left, bool need, Expression<Func<T, bool>> right)
        {
            if (need)
            {
                return left.And(right);
            }
            else
            {
                return left;
            }
        }

        /// 
        /// 符合条件则Or
        /// 
        /// 实体类型
        /// 原表达式
        /// 是否符合条件
        /// 新表达式
        /// 
        public static Expression<Func<T, bool>> OrIf<T>(this Expression<Func<T, bool>> left, bool need, Expression<Func<T, bool>> right)
        {
            if (need)
            {
                return left.Or(right);
            }
            else
            {
                return left;
            }
        }

        #endregion

        #region 拓展BuildExtendSelectExpre方法

        /// 
        /// 组合继承属性选择表达式树,无拓展参数
        /// TResult将继承TBase的所有属性
        /// 
        /// 原数据类型
        /// 返回类型
        /// 拓展表达式
        /// 
        public static Expression<Func<TBase, TResult>> BuildExtendSelectExpre<TBase, TResult>(this Expression<Func<TBase, TResult>> expression)
        {
            return GetExtendSelectExpre<TBase, TResult, Func<TBase, TResult>>(expression);
        }

        /// 
        /// 组合继承属性选择表达式树,1个拓展参数
        /// TResult将继承TBase的所有属性
        /// 
        /// 原数据类型
        /// 拓展类型1
        /// 返回类型
        /// 拓展表达式
        /// 
        public static Expression<Func<TBase, T1, TResult>> BuildExtendSelectExpre<TBase, T1, TResult>(this Expression<Func<TBase, T1, TResult>> expression)
        {
            return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, TResult>>(expression);
        }

        /// 
        /// 组合继承属性选择表达式树,2个拓展参数
        /// TResult将继承TBase的所有属性
        /// 
        /// 原数据类型
        /// 拓展类型1
        /// 拓展类型2
        /// 返回类型
        /// 拓展表达式
        /// 
        public static Expression<Func<TBase, T1, T2, TResult>> BuildExtendSelectExpre<TBase, T1, T2, TResult>(this Expression<Func<TBase, T1, T2, TResult>> expression)
        {
            return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, T2, TResult>>(expression);
        }

        /// 
        /// 组合继承属性选择表达式树,3个拓展参数
        /// TResult将继承TBase的所有属性
        /// 
        /// 原数据类型
        /// 拓展类型1
        /// 拓展类型2
        /// 拓展类型3
        /// 返回类型
        /// 拓展表达式
        /// 
        public static Expression<Func<TBase, T1, T2, T3, TResult>> BuildExtendSelectExpre<TBase, T1, T2, T3, TResult>(this Expression<Func<TBase, T1, T2, T3, TResult>> expression)
        {
            return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, T2, T3, TResult>>(expression);
        }

        /// 
        /// 组合继承属性选择表达式树,4个拓展参数
        /// TResult将继承TBase的所有属性
        /// 
        /// 原数据类型
        /// 拓展类型1
        /// 拓展类型2
        /// 拓展类型3
        /// 拓展类型4
        /// 返回类型
        /// 拓展表达式
        /// 
        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)
        {
            return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, T2, T3, T4, TResult>>(expression);
        }

        /// 
        /// 组合继承属性选择表达式树,5个拓展参数
        /// TResult将继承TBase的所有属性
        /// 
        /// 原数据类型
        /// 拓展类型1
        /// 拓展类型2
        /// 拓展类型3
        /// 拓展类型4
        /// 拓展类型5
        /// 返回类型
        /// 拓展表达式
        /// 
        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)
        {
            return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, T2, T3, T4, T5, TResult>>(expression);
        }

        /// 
        /// 组合继承属性选择表达式树,6个拓展参数
        /// TResult将继承TBase的所有属性
        /// 
        /// 原数据类型
        /// 拓展类型1
        /// 拓展类型2
        /// 拓展类型3
        /// 拓展类型4
        /// 拓展类型5
        /// 拓展类型6
        /// 返回类型
        /// 拓展表达式
        /// 
        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)
        {
            return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, T2, T3, T4, T5, T6, TResult>>(expression);
        }

        /// 
        /// 组合继承属性选择表达式树,7个拓展参数
        /// TResult将继承TBase的所有属性
        /// 
        /// 原数据类型
        /// 拓展类型1
        /// 拓展类型2
        /// 拓展类型3
        /// 拓展类型4
        /// 拓展类型5
        /// 拓展类型6
        /// 拓展类型7
        /// 返回类型
        /// 拓展表达式
        /// 
        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)
        {
            return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, T2, T3, T4, T5, T6, T7, TResult>>(expression);
        }

        /// 
        /// 组合继承属性选择表达式树,8个拓展参数
        /// TResult将继承TBase的所有属性
        /// 
        /// 原数据类型
        /// 拓展类型1
        /// 拓展类型2
        /// 拓展类型3
        /// 拓展类型4
        /// 拓展类型5
        /// 拓展类型6
        /// 拓展类型7
        /// 拓展类型8
        /// 返回类型
        /// 拓展表达式
        /// 
        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)
        {
            return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, T2, T3, T4, T5, T6, T7, T8, TResult>>(expression);
        }

        /// 
        /// 组合继承属性选择表达式树,9个拓展参数
        /// TResult将继承TBase的所有属性
        /// 
        /// 原数据类型
        /// 拓展类型1
        /// 拓展类型2
        /// 拓展类型3
        /// 拓展类型4
        /// 拓展类型5
        /// 拓展类型6
        /// 拓展类型7
        /// 拓展类型8
        /// 拓展类型9
        /// 返回类型
        /// 拓展表达式
        /// 
        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)
        {
            return GetExtendSelectExpre<TBase, TResult, Func<TBase, T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>>(expression);
        }

        #endregion

        #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>();
            oldExpression.Bindings.ToList().ForEach(aBinding =>
            {
                existsProperties.Add(aBinding.Member.Name);
            });

            List<MemberBinding> newBindings = new List<MemberBinding>();
            typeof(TBase).GetProperties().Where(x => !existsProperties.Contains(x.Name)).ToList().ForEach(aProperty =>
            {
                if (typeof(TResult).GetMembers().Any(x => x.Name == aProperty.Name))
                {
                    MemberBinding newMemberBinding = null;
                    var valueExpre = Expression.Property(oldParamters[0], aProperty.Name);
                    if (typeof(TBase).IsAssignableFrom(typeof(TResult)))
                    {
                        newMemberBinding = Expression.Bind(aProperty, valueExpre);
                    }
                    else
                    {
                        newMemberBinding = Expression.Bind(typeof(TResult).GetProperty(aProperty.Name), valueExpre);
                    }
                    newBindings.Add(newMemberBinding);
                }
            });

            newBindings.AddRange(oldExpression.Bindings);

            var body = Expression.MemberInit(newBody, newBindings.ToArray());
            var resExpression = Expression.Lambda<TDelegate>(body, oldParamters);

            return resExpression;
        }

        #endregion
    }

    /// 
    /// 继承ExpressionVisitor类,实现参数替换统一
    /// 
    internal class ParameterReplaceVisitor : System.Linq.Expressions.ExpressionVisitor
    {
        public ParameterReplaceVisitor(ParameterExpression paramExpr)
        {
            _parameter = paramExpr;
        }

        //新的表达式参数
        private readonly ParameterExpression _parameter;

        protected override Expression VisitParameter(ParameterExpression p)
        {
            if (p.Type == _parameter.Type)
            {
                return _parameter;
            }

            else
            {
                return p;
            }
        }
    }
}

你可能感兴趣的:(#,C#类库/扩展方法,c#,asp.net,后端)