Files
tnb.server/common/Tnb.Common/Extension/LambdaExpressionExtensions.cs
yang.lee 71c4dbd254 1、新增预任务生成帮助类
2、齐套配送接口参数调整
2023-11-23 14:13:03 +08:00

170 lines
7.2 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);
}
public static object GetPropertyValue<T>(this T obj, string propertyName)
{
if (!ProperyGet<T>.ValueFactories.TryGetValue(propertyName, out var getDateValue))
{
getDateValue = ProperyGet<T>.PropertyGetValue(propertyName);
ProperyGet<T>.ValueFactories.Add(propertyName, getDateValue);
}
return getDateValue(obj);
}
/// <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);
}
/// <summary>
/// or扩展
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="first"></param>
/// <param name="second"></param>
/// <returns></returns>
public static Expression<Func<T1, T2, T3, bool>> Or<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.Or);
}
}
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();
}
}
public class ProperyGet<T> //where T : class, new()
{
public static Dictionary<string, Func<T, object>> ValueFactories = new Dictionary<string, Func<T, object>>(StringComparer.OrdinalIgnoreCase);
public static Func<T, Object> PropertyGetValue(string name)
{
var parameterExp = Expression.Parameter(typeof(T), "entity");
var propertyExp = Expression.Property(parameterExp, name);
var castExp = Expression.Convert(propertyExp, typeof(object));
var lambda = Expression.Lambda<Func<T, object>>(castExp, parameterExp);
return lambda.Compile();
}
}
}