首页 > 解决方案 > 如何从字符串值构建表达式树

问题描述

我在别处问过一个具体问题,但在没有回应和一些调查之后,我把它归结为更通用的东西,但我仍在努力构建表达式树。

我正在使用第三方库,它使用接口和扩展方法进行一些映射。这些映射被指定为表达式树,我想做的是从字符串值构建该表达式树。

扩展方法签名:

public static T UpdateGraph<T>(this DbContext context, T entity, Expression<Func<IUpdateConfiguration<T>, object>> mapping = null, bool allowDelete = true) where T : class, new();

IUpdateConfiguration 接口只是一个标记接口,但具有以下扩展方法:

public static class UpdateConfigurationExtensions
{
    public static IUpdateConfiguration<T> OwnedCollection<T, T2>(this IUpdateConfiguration<T> config, Expression<Func<T, ICollection<T2>>> expression);
    public static IUpdateConfiguration<T> OwnedCollection<T, T2>(this IUpdateConfiguration<T> config, Expression<Func<T, ICollection<T2>>> expression, Expression<Func<IUpdateConfiguration<T2>, object>> mapping);
    public static IUpdateConfiguration<T> OwnedEntity<T, T2>(this IUpdateConfiguration<T> config, Expression<Func<T, T2>> expression);
    public static IUpdateConfiguration<T> OwnedEntity<T, T2>(this IUpdateConfiguration<T> config, Expression<Func<T, T2>> expression, Expression<Func<IUpdateConfiguration<T2>, object>> mapping);
}
  

使用示例实体:

public class Person
{
  public Car Car {get;set;}
  public House House {get;set;}
}

所以正常的显式用法是:

dbContext.UpdateGraph(person, mapping => mapping.OwnedEntity(p => p.House).OwnedEntity(p=> p.Car));

我需要做的是从属性名称列表中建立该映射,

var props = {"Car","House"}

dbContext.UpdateGraph(person, buildExpressionFromStrings<Person>(props);

我到目前为止:


static Expression<Func<IUpdateConfiguration<t>, object>> buildExpressionFromStrings<t>(IEnumerable<string> props)
{
   foreach (var s in props)
   {
        var single = buildExpressionFromString(s);
        somehow add this to chaining overall expression

    }      
}

static Expression<Func<IUpdateConfiguration<t>, object>> buildExpressionFromString<t>(string prop)
            {
                var ownedChildParam = Expression.Parameter(typeof(t));

                var ownedChildExpression = Expression.PropertyOrField(ownedChildParam, prop);

                var ownedChildLam = Expression.Lambda(ownedChildExpression, ownedChildParam);

                // Up to here I think we've built the (o => o.Car) part of map => map.OwnedEntity(o => o.Car)
// So now we need to build the map=>map.OwnedEntity(ownedChildLam) part, by calling Expression.Call I believe, but here I'm getting confused.
            }

实际上,现实世界的代码比这更复杂(需要处理递归和子属性/映射),但我认为一旦我为一个级别构建了表达式,我就可以对其进行排序。一天多来,我一直在努力解决这个问题……为了提供一些上下文,我使用实体框架和一些配置来定义聚合根。

标签: c#entity-frameworklinqexpression-trees

解决方案


几件事可提。这里

mapping => mapping.OwnedEntity(p => p.House).OwnedEntity(p=> p.Car)

lambda 表达式主体的部分不是 lambda 表达式,而只是链式方法调用表达式,第一个使用 lambda 表达式参数,下一个使用前一个结果。

其次,扩展方法只是静态方法调用 C# 糖,在表达式树中,它们必须作为静态方法“调用”。

所以,建立一个电话

public static IUpdateConfiguration<T> OwnedEntity<T, T2>(this IUpdateConfiguration<T> config, Expression<Func<T, T2>> expression);

可能是这样的

static Expression BuildConfigurationCall<T>(Expression config, string propertyName)
{
    var parameter = Expression.Parameter(typeof(T), "it");
    var property = Expression.Property(parameter, propertyName);
    var selector = Expression.Lambda(property, parameter);
    return Expression.Call(
        typeof(UpdateConfigurationExtensions),
        nameof(UpdateConfigurationExtensions.OwnedEntity),
        new [] { typeof(T), property.Type },
        config,
        selector);
}

并且有问题的 lambda 表达式将是:

static Expression<Func<IUpdateConfiguration<T>, object>> BuildConfigurationExpression<T>(IEnumerable<string> propertyNames)
{
    var parameter = Expression.Parameter(typeof(IUpdateConfiguration<T>), "config");
    var body = propertyNames.Aggregate((Expression)parameter,
        (config, propertyName) => BuildConfigurationCall<T>(config, propertyName));
    return Expression.Lambda<Func<IUpdateConfiguration<T>, object>>(body, parameter);
}

推荐阅读