(에센스) 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; } } } }

좋은 웹페이지 즐겨찾기