C# 编译器错误:无法转换 lambda 表达式

发布于 2024-10-22 00:08:57 字数 1539 浏览 1 评论 0 原文

我尝试使用 Lambda 表达式和反射来获取成员层次结构名称(而不是使用文本常量),以便在我的控件绑定信息无效时强制执行编译时错误。

这是一个 ASP.NET MVC 项目,但它不是 MVC 特定的问题 AFAIK。编辑:具体来说,我希望以下内容评估为 true:

string fullname = GetExpressionText(model => model.Locations.PreferredAreas);
"Locations.PreferredAreas" == fullname;

相反,我收到编译错误:

错误 4:无法将 lambda 表达式转换为类型 “System.Linq.Expressions.LambdaExpression”,因为它不是委托类型。

为什么该参数在下面的第二种情况下起作用,而在第一种情况下不起作用?

// 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);

以下是 ASP.NET MVC Codeplex 项目中的相关代码。在我看来,它将相同的参数传递给相同的方法:

// 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...

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.

This is in an ASP.NET MVC project, but it's not an MVC-specific question AFAIK. EDIT: Specifically, I want the following to evaluate to 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.

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. 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独难免 2024-10-29 00:08:57

错误信息是正确的。 lambda 可以转换为兼容委托类型 D,或转换为兼容委托类型的表达式 ExpressionExpression> 就是其中之一。 “LambdaExpression”都不是这些。因此,在尝试将 lambda 转换为 LambdaExpression,而不是实际的表达式树类型时,您会收到错误消息。那里一定有一个代表

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>. Expression<Func<TM, TP>> is one of those. "LambdaExpression" is neither of those. Therefore you get an error trying to convert the lambda to LambdaExpression, but not to an actual expression tree type. There has to be a delegate in there somewhere.

£烟消云散 2024-10-29 00:08:57

在尝试修复 lambda 表达式之前,请确保已添加以下引用:

System.Linq;
System.Linq.Expressions;

缺少这些引用也可能会导致相同的错误(“无法将 lambda 表达式转换为类型“System.Linq.Expressions.Lambda Expression”,因为它不是委托类型” “)。

Before trying to fix the lambda expressions, be sure that the following references have already been added:

System.Linq;
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").

孤君无依 2024-10-29 00:08:57

我认为你应该尝试使用这样的辅助方法:

public static string GetExpressionText<M, P>(this M model, Expression<Func<M, P>> ex)
{
    return GetExpressionText(ex);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文