You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

120 lines
3.5 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace EC.Utils
{
/// <summary>
/// ExtLinq
/// 版 本:V3.0.0
/// 版 权:EasyCode
/// 作 者:LXC
/// </summary>
public static partial class ExtLinq
{
public static Expression Property(this Expression expression, string propertyName)
{
return Expression.Property(expression, propertyName);
}
public static Expression<Func<T, bool>> True<T>()
{
return param => true;
}
public static Expression<Func<T, bool>> False<T>()
{
return param => false;
}
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.AndAlso);
}
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.OrElse);
}
public static Expression<Func<T, bool>> ToLambda<T>(string propertyName, string propertyValue)
{
var parameter = Expression.Parameter(typeof(T), "t");
return Expression.Lambda<Func<T, bool>>(Expression.Equal(parameter.Property(propertyName), Expression.Constant(propertyValue)), parameter);
}
public static Expression<T> ToLambda<T>(this Expression body, params ParameterExpression[] parameters)
{
return Expression.Lambda<T>(body, parameters);
}
public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
{
var map = first.Parameters
.Select((f, i) => new { f, s = second.Parameters[i] })
.ToDictionary(p => p.s, p => p.f);
var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
}
public static string GetPropertyName<T, TProp>(Expression<Func<T, TProp>> expression)
{
var name = "";
if (expression.Body is UnaryExpression)
{
name = ((MemberExpression)((UnaryExpression)expression.Body).Operand).Member.Name;
}
else if (expression.Body is MemberExpression)
{
name = ((MemberExpression)expression.Body).Member.Name;
}
else if (expression.Body is ParameterExpression)
{
name = ((ParameterExpression)expression.Body).Type.Name;
}
return name;
}
private static bool IsNullableType(Type theType)
{
return (theType.IsGenericType && theType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)));
}
private class ParameterRebinder : ExpressionVisitor
{
private readonly Dictionary<ParameterExpression, ParameterExpression> map;
/// <summary>
/// Initializes a new instance of the <see cref="ParameterRebinder"/> class.
/// </summary>
/// <param name="map">The map.</param>
private ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
{
this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
}
/// <summary>
/// Replaces the parameters.
/// </summary>
/// <param name="map">The map.</param>
/// <param name="exp">The exp.</param>
/// <returns>Expression</returns>
public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
{
return new ParameterRebinder(map).Visit(exp);
}
protected override Expression VisitParameter(ParameterExpression p)
{
ParameterExpression replacement;
if (map.TryGetValue(p, out replacement))
{
p = replacement;
}
return base.VisitParameter(p);
}
}
}
}