用于创建具有可变数量的泛型类型参数的元组的表达式

时间:2017-11-11 10:00:29

标签: c# linq-expressions

我正在尝试构建一个表达式,用于创建具有可变数量泛型类型参数的通用Tuple<>实例。

生成的Tuple<>实例的想法是基于其上具有KeyAttribute的属性为实体类型动态创建复合键值。然后,组合密钥将用作Dictionary<object, TEntity>中的密钥。因此,应该为某个实体类型构建lambda表达式,并调用lambda,传递TEntity的实例以获取Tuple<>形式的复合键。

示例实体模型

public class MyEntityModel
{
    [Key]
    public string Key1 { get; set; }
    [Key]
    public Guid Key2 { get; set; }
    public int OtherProperty { get; set; }
}

应该做什么表达

public Func<MyEntityModel, object> BuildKeyFactory()
{
    // This is how the LambdaExpression should look like, but then for a generic entity type instead of fixed to MyEntityModel
    return new Func<MyEntityModel, object>(entity => new Tuple<string, Guid>(entity.Key1, entity.Key2));
}

但当然,实体模型必须是通用类型。

到目前为止我有什么

public Func<TEntity, object> BuildKeyFactory<TEntity>()
{
    var entityType = typeof(TEntity);

    // Get properties that have the [Key] attribute
    var keyProperties = entityType.GetProperties(BindingFlags.Instance | BindingFlags.Public)
        .Where(x => x.GetCustomAttribute(typeof(KeyAttribute)) != null)
        .ToArray();

    var tupleType = Type.GetType($"System.Tuple`{keyProperties.Length}");
    if (tupleType == null) throw new InvalidOperationException($"No tuple type found for {keyProperties.Length} generic arguments");

    var keyPropertyTypes = keyProperties.Select(x => x.PropertyType).ToArray();
    var tupleConstructor = tupleType.MakeGenericType(keyPropertyTypes).GetConstructor(keyPropertyTypes);
    if (tupleConstructor == null) throw new InvalidOperationException($"No tuple constructor found for key in {entityType.Name} entity");

    // The following part is where I need some help with...
    var newTupleExpression = Expression.New(tupleConstructor, keyProperties.Select(x => ????));

    return Expression.Lambda<Func<TEntity, object>>(????).Compile();
}

正如您所看到的,我无法弄清楚我是如何创建属性表达式以传递给Expression.New()调用的(可能是Expression.MakeMemberAccess(Expression.Property())但不是知道如何从lambda参数传递TEntity实例)以及如何链接&#39;这与Expression.Lambda电话有关。任何帮助将非常感谢!

1 个答案:

答案 0 :(得分:3)

你很亲密。

// we need to build entity => new Tuple<..>(entity.Property1, entity.Property2...)
// arg represents "entity" above
var arg = Expression.Parameter(typeof(TEntity));
// The following part is where I need some help with...
// Expression.Property(arg, "name) represents "entity.Property1" above
var newTupleExpression = Expression.New(tupleConstructor, keyProperties.Select(c => Expression.Property(arg, c)));
return Expression.Lambda<Func<TEntity, object>>(newTupleExpression, arg).Compile();