[英]Expression to create a Tuple with variable number of generic type arguments
我正在尝试构建一个表达式,用于创建具有可变数量的泛型类型参数的通用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
实例)以及我如何使用Expression.Lambda
调用“链接”它。 任何帮助将非常感谢!
你很亲密。
// 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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.