用于创建具有可变数量的泛型类型参数的元组的表达式
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
调用 'chain' 这个。任何帮助将不胜感激!
你很接近。
// 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();
我正在尝试构建一个表达式来创建具有可变数量的泛型类型参数的泛型 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
调用 'chain' 这个。任何帮助将不胜感激!
你很接近。
// 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();