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
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;
}
}
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方法
public static Expression<Func<TBase, TResult>> BuildExtendSelectExpre<TBase, TResult>(this Expression<Func<TBase, TResult>> expression)
{
return GetExtendSelectExpre<TBase, TResult, Func<TBase, TResult>>(expression);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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
}
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;
}
}
}
}