简体   繁体   English

如何创建programmaticaly Expression.Lambda <Func<TEntity, TProperty> &gt;带变量类型?

[英]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.

相关问题 如何创建表达式<Func<TModel, TProperty> &gt; 没有 TProperty 类型显式 - How to create Expression<Func<TModel, TProperty>> without TProperty type explicit 如何将多个属性传递给表达式<Func<TEntity, IEnumerable<TProperty> &gt;&gt; - How to pass multiple properties to Expression<Func<TEntity, IEnumerable<TProperty>>> 如何创建表达式 <Func<TModel, TProperty> &gt;; - How to create Expression<Func<TModel, TProperty>>; 使用 Expression.Lambda 制作 lambda 表达式时如何使用变量<func<> &gt;() </func<> - How to use a variable when making a lambda expression using Expression.Lambda<Func<>>() 动态创建Expression.Lambda的通用类型 - Dynamically create generic type of an Expression.Lambda 如果直到运行时才知道类型,如何创建Expression.Lambda? - How to create a Expression.Lambda when a type is not known until runtime? 如何创建类型为Expression.Lambda的Lambda表达式 <Action<Controller> &gt;? 动态地 - How do I create a lambda expression of type Expression.Lambda<Action<Controller>>? dynamically Expression.Lambda:变量&#39;&#39;&#39;类型&#39;&#39;引用范围&#39;&#39;,但它没有定义 - Expression.Lambda: Variable 'x' of type '' referenced from scope '', but it is not defined 转换表达式 <Func<TEntity, IEnumerable<TProperty> &gt;&gt; valueSelector,将TValue []的值传递给表达式 <Func<TElement, bool> &gt; - Converting Expression<Func<TEntity, IEnumerable<TProperty>>> valueSelector, TValue[] values to Expression<Func<TElement, bool>> 如何将Expression.Lambda附加到任何所有者类型? - How to attach Expression.Lambda to ANY owner type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM