【发布时间】:2010-08-19 01:36:56
【问题描述】:
我有两个这样定义的表达式树:
private Expression<Func<TEntity, TPropertyResult>> PropertyAccessor { get; set; }
和
private Expression<Func<TPropertyResult, bool>> TestExpression { get; set; }
我需要创建一个新的表达式树,结果相当于:
var expression = p => this.TestExpression(this.PropertyAccessor(p));
使用Expression.Invoke(this.TestExpression, this.PropertyAccessor)时,出现以下错误
{"表达式类型 'System.Func`2[MyEntity,System.String]' 不能用于类型参数 'System.String'"}
TPropertyResult 在我的测试中是一个字符串。
我尝试使用Expression.Call 或Expression.Invoke。没有运气。我应该使用什么?
【问题讨论】:
-
编译器错误是什么?此外,代码不是那么可读。您确定 .Net 2.0 的内容不足以完成您想要完成的任务吗?
-
使用 Invoke 时,提示:
{"Expression of type 'System.Func2[MyEntity,System.String]' cannot be used for parameter of type 'System.String'"}. That is when I try to specify a string as theTPropertyResult`,但是问题不限于字符串。
标签: c# lambda expression-trees