131 lines
5.6 KiB
C#
131 lines
5.6 KiB
C#
using System.Linq.Expressions;
|
|
|
|
namespace JNPF.Common.Extension
|
|
{
|
|
public static class LambdaExpressionExtensions
|
|
{
|
|
public static void PropertySetValue<T>(this T instance, string propertyName, string value)
|
|
{
|
|
if (!PropertySet<T>.ValueFactories.TryGetValue(propertyName, out Action<object, object> setAction))
|
|
{
|
|
setAction = PropertySet<T>.CreateSetPropertyValueAction(propertyName);
|
|
PropertySet<T>.ValueFactories.Add(propertyName, setAction);
|
|
}
|
|
setAction(instance, value);
|
|
}
|
|
|
|
public static void PropertySetValue<T>(this T instance, string propertyName, object value)
|
|
{
|
|
if (!PropertySet<T>.ValueFactories2.TryGetValue(propertyName, out Action<T, object> setAction))
|
|
{
|
|
setAction = PropertySet<T>.CreateSetPropertyValueAction2(propertyName);
|
|
PropertySet<T>.ValueFactories2.Add(propertyName, setAction);
|
|
}
|
|
setAction(instance, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lambda表达式拼接
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="first"></param>
|
|
/// <param name="second"></param>
|
|
/// <param name="merge"></param>
|
|
/// <returns></returns>
|
|
public static Expression<Func<T1, T2, T3, bool>> Compose<T1, T2, T3>(this Expression<Func<T1, T2, T3, bool>> first, Expression<Func<T1, T2, T3, bool>> second,
|
|
Func<Expression, Expression, Expression> merge)
|
|
{
|
|
// build parameter map (from parameters of second to parameters of first)
|
|
var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f);
|
|
// replace parameters in the second lambda expression with parameters from the first
|
|
var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
|
|
// apply composition of lambda expression bodies to parameters from the first expression
|
|
return Expression.Lambda<Func<T1, T2, T3, bool>>(merge(first.Body, secondBody), first.Parameters);
|
|
}
|
|
/// <summary>
|
|
/// and扩展
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="first"></param>
|
|
/// <param name="second"></param>
|
|
/// <returns></returns>
|
|
public static Expression<Func<T1,T2,T3, bool>> And<T1, T2, T3>(this Expression<Func<T1, T2, T3, bool>> first, Expression<Func<T1, T2, T3, bool>> second)
|
|
{
|
|
if (first.IsNull())
|
|
{
|
|
first = second;
|
|
return first;
|
|
}
|
|
|
|
return first.Compose(second, Expression.And);
|
|
}
|
|
}
|
|
|
|
|
|
public class ParameterRebinder : ExpressionVisitor
|
|
{
|
|
private readonly Dictionary<ParameterExpression, ParameterExpression> map;
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="map"></param>
|
|
public ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
|
|
{
|
|
this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="map"></param>
|
|
/// <param name="exp"></param>
|
|
/// <returns></returns>
|
|
public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
|
|
{
|
|
return new ParameterRebinder(map).Visit(exp);
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="p"></param>
|
|
/// <returns></returns>
|
|
protected override Expression VisitParameter(ParameterExpression p)
|
|
{
|
|
ParameterExpression replacement;
|
|
if (map.TryGetValue(p, out replacement))
|
|
{
|
|
p = replacement;
|
|
}
|
|
return base.VisitParameter(p);
|
|
}
|
|
}
|
|
public class PropertySet<T>
|
|
{
|
|
public static Dictionary<string, Action<object, object>> ValueFactories = new Dictionary<string, Action<object, object>>(StringComparer.OrdinalIgnoreCase);
|
|
public static Dictionary<string, Action<T, object>> ValueFactories2 = new Dictionary<string, Action<T, object>>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
public static Action<object, object> CreateSetPropertyValueAction(string propertyName)
|
|
{
|
|
var property = typeof(T).GetProperty(propertyName);
|
|
var target = Expression.Parameter(typeof(object));
|
|
var propertyValue = Expression.Parameter(typeof(object));
|
|
var castTarget = Expression.Convert(target, typeof(T));
|
|
var castPropertyValue = Expression.Convert(propertyValue, property!.PropertyType);
|
|
var setPropertyValue = Expression.Call(castTarget, property.GetSetMethod()!, castPropertyValue);
|
|
return Expression.Lambda<Action<object, object>>(setPropertyValue, target, propertyValue).Compile();
|
|
}
|
|
|
|
public static Action<T, object> CreateSetPropertyValueAction2(string propertyName)
|
|
{
|
|
var property = typeof(T).GetProperty(propertyName);
|
|
var target = Expression.Parameter(typeof(T));
|
|
var propExp = Expression.Property(target, propertyName);
|
|
var propertyValue = Expression.Parameter(typeof(object));
|
|
var castPropertyValue = Expression.Convert(propertyValue, property!.PropertyType);
|
|
var assignExp = Expression.Assign(propExp, castPropertyValue);
|
|
|
|
return Expression.Lambda<Action<T, object>>(assignExp, new[] { target, propertyValue }).Compile();
|
|
}
|
|
|
|
}
|
|
}
|