[英]C# compiler error: Cannot convert lambda expression
I'm trying to use a Lambda expression and reflection to get a member hierarchical name (rather than using a text constant), to enforce compile-time errors if my control binding information is invalid. 我正在尝试使用Lambda表达式和反射来获取成员层次结构名称(而不是使用文本常量),以在我的控件绑定信息无效时强制执行编译时错误。
This is in an ASP.NET MVC project, but it's not an MVC-specific question AFAIK. 这在ASP.NET MVC项目中,但不是MVC特定的问题AFAIK。 EDIT: Specifically, I want the following to evaluate to true:
编辑:具体来说,我希望以下内容评估为true:
string fullname = GetExpressionText(model => model.Locations.PreferredAreas);
"Locations.PreferredAreas" == fullname;
Instead I get a compile error: 相反,我得到一个编译错误:
Error 4: Cannot convert lambda expression to type 'System.Linq.Expressions.LambdaExpression' because it is not a delegate type.
错误4:无法将lambda表达式转换为类型'System.Linq.Expressions.LambdaExpression',因为它不是委托类型。
Why does the parameter work in the second case below, but not the first? 为什么参数在下面的第二种情况下有效,而在第一种情况下无效?
// This doesn't compile:
string tb1 = System.Web.Mvc.ExpressionHelper.
GetExpressionText(model => model.Locations.PreferredAreas);
// But this does:
MvcHtmlString tb2 =
Html.TextBoxFor(model => model.Locations.PreferredAreas);
Here's the relevant code from the ASP.NET MVC Codeplex project. 这是ASP.NET MVC Codeplex项目中的相关代码。 It looks to me like it passes the same parameter through to the same method:
在我看来,它将相同的参数传递给相同的方法:
// MVC extension method
public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes) {
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
return TextBoxHelper(
htmlHelper,
metadata,
metadata.Model,
ExpressionHelper.GetExpressionText(expression),
htmlAttributes);
}
// MVC utility method
public static string GetExpressionText(LambdaExpression expression) {
// Split apart the expression string for property/field accessors to create its name
// etc...
The error message is correct. 错误消息是正确的。 A lambda can be converted to a compatible delegate type, D, or to an expression-of-compatible-delegate-type
Expression<D>
. 可以将lambda转换为兼容的委托类型D或兼容的委托类型的
Expression<D>
。 Expression<Func<TM, TP>>
is one of those. Expression<Func<TM, TP>>
是其中之一。 "LambdaExpression" is neither of those. “ LambdaExpression”都不是。 Therefore you get an error trying to convert the lambda to LambdaExpression, but not to an actual expression tree type.
因此,您在尝试将lambda转换为LambdaExpression而不是将其转换为实际的表达式树类型时遇到错误。 There has to be a delegate in there somewhere.
某处必须有一位代表 。
Before trying to fix the lambda expressions, be sure that the following references have already been added: 在尝试修复lambda表达式之前,请确保已添加以下参考:
System.Linq;
System.Linq;
System.Linq.Expressions;System.Linq.Expressions;
The lack of these references may cause the same error as well ( "Cannot convert lambda expression to type 'System.Linq.Expressions.Lambda Expression' because it is not a delegate type" ). 缺少这些引用也可能导致相同的错误( “因为它不是委托类型,所以无法将lambda表达式转换为类型'System.Linq.Expressions.Lambda Expression'” )。
Hope this helps... 希望这可以帮助...
I think you should try to use a helper method like that: 我认为您应该尝试使用这样的辅助方法:
public static string GetExpressionText<M, P>(this M model, Expression<Func<M, P>> ex)
{
return GetExpressionText(ex);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.