[英]How Create programmaticaly Expression.Lambda<Func<TEntity, TProperty>> with variable Type?
I want to create below code 我想创建下面的代码
var lambda = Expression.Lambda<Func<TEntity, TProperty>>(expName, entity);
but TProperty type is variable and change in loop and i can get type of this : 但TProperty类型是可变的并且在循环中改变,我可以得到这种类型:
var nameType = typeof(TEntity).GetProperty(name);
I want to have like this 我想要这样
var lambda = Expression.Lambda<Func<TEntity, nameType>>(expName, entity);
Can I create this ? 我可以创建吗?
You can use Expression.Lamdba(Type, Expression, params ParameterExpression[])
- you'd use typeof(Func<,>).MakeGenericType(typeof(TEntity), nameType)
to create the relevant type. 您可以使用Expression.Lamdba(Type, Expression, params ParameterExpression[])
- 您将使用typeof(Func<,>).MakeGenericType(typeof(TEntity), nameType)
来创建相关类型。
That will just give you a LambdaExpression
though. 那只会给你一个LambdaExpression
。 It's not clear what you're trying to do with the result, but you won't have a strongly-typed expression you can invoke. 目前还不清楚你要对结果做什么,但你不会有一个可以调用的强类型表达式。 (It will build the right delegate type when you compile it though.) (当你编译它时,它将构建正确的委托类型。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.