(에센스) 2020년 6월 26일 C#라이브러리 Expression(확장 방법)
55987 단어 #C# 라이브러리/확장 방법c#asp.net백엔드
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;
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Rails Turbolinks를 페이지 단위로 비활성화하는 방법원래 Turobolinks란? Turbolinks는 링크를 생성하는 요소인 a 요소의 클릭을 후크로 하고, 이동한 페이지를 Ajax에서 가져옵니다. 그 후, 취득 페이지의 데이터가 천이 전의 페이지와 동일한 것이 있...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.