lambda 表达式语法与 LambdaExpression 类

发布于 2024-10-19 19:50:31 字数 906 浏览 2 评论 0原文

这行代码尝试将 lambda 表达式分配给 LambaExpression 类型变量,

LambdaExpression expr = n => n;

失败并显示编译错误消息:

无法转换 lambda 要键入的表达式 'System.Linq.Expressions.LambdaExpression' 因为它不是代表 类型

所以它需要是委托类型。从概念上讲,这对我来说似乎很奇怪,因为我可以使用像这样的工厂方法构建一个 LambdaExpression 实例。

Factory Lambda 来自 MSDN

LambdaExpression lambdaExpr = Expression.Lambda(
    Expression.Add(
        paramExpr,
        Expression.Constant(1)
    ),
    new List<ParameterExpression>() { paramExpr }
);

并且这不是委托类型。

这让我们想知道为什么 lambda 到 LambaExpression 不能工作?

This line of code that tries to assign a lambda expression to a LambaExpression typed variable,

LambdaExpression expr = n => n;

it fails with compile error message:

Cannot convert lambda
expression to type
'System.Linq.Expressions.LambdaExpression'
because it is not a delegate
type

So it needs to be a delegate type. Conceptually it seems odd to me because I can build out a LambdaExpression instance using a factory method like so.

Factory Lambda from MSDN

LambdaExpression lambdaExpr = Expression.Lambda(
    Expression.Add(
        paramExpr,
        Expression.Constant(1)
    ),
    new List<ParameterExpression>() { paramExpr }
);

and that's not a delegate type.

It makes we wonder why lambda to LambaExpression cannot work?

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

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

发布评论

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

评论(4

灰色世界里的红玫瑰 2024-10-26 19:50:31

嗯,这确实有效:

Expression<Func<int, int>> exp = n => n;
LambdaExpression lambda = exp;

请注意Expression< ;TDelegate> 派生自 LambdaExpression

我认为您不能仅使用 LambdaExpression 作为类型的原因是无法推断出 n 的类型(在您的示例中)。

考虑一下这样一个事实:您也不能这样做,原因基本相同:

// What is this? An Action? A ThreadStart? What?
Delegate d = () => Console.WriteLine("Hi!");

而您可以这样做:

Action a = () => Console.WriteLine("Hi!");
Delegate d = a;

本质上是同一件事。

Well, this does work:

Expression<Func<int, int>> exp = n => n;
LambdaExpression lambda = exp;

Note that Expression<TDelegate> derives from LambdaExpression.

I think the reason you can't just use LambdaExpression as the type is that then the type of n (in your example) could not be inferred.

Consider the fact that you also can't do this, for basically the same reason:

// What is this? An Action? A ThreadStart? What?
Delegate d = () => Console.WriteLine("Hi!");

Whereas you can do this:

Action a = () => Console.WriteLine("Hi!");
Delegate d = a;

It's essentially the same thing.

万人眼中万个我 2024-10-26 19:50:31

因为 LambdaExpression 是一种在运行时生成 lambda 表达式的方法,其中 n => n 在编译时转换为生成的类。

简而言之:它们是做同一件事的两个不同的东西,但不能一起使用。

Because LambdaExpression is a way to generate lambda expressions at runtime, where as n => n gets converted to a generated class at compile time.

In short: they are two different things to do the same thing, but can't be used together.

风吹雪碎 2024-10-26 19:50:31

引用MSDN
LambdaExpression 类型表示表达式树形式的 lambda 表达式。 Expression 类型派生自 LambdaExpression,并更明确地捕获 lambda 表达式的类型,也可用于表示 lambda 表达式。在运行时,表示 lambda 表达式的表达式树节点始终为 Expression 类型。

LambdaExpression 的 NodeType 属性的值为 Lambda。

使用 Lambda 工厂方法创建 LambdaExpression 对象。

To quote MSDN
The LambdaExpression type represents a lambda expression in the form of an expression tree. The Expression type, which derives from LambdaExpression and captures the type of the lambda expression more explicitly, can also be used to represent a lambda expression. At runtime, an expression tree node that represents a lambda expression is always of type Expression.

The value of the NodeType property of a LambdaExpression is Lambda.

Use the Lambda factory methods to create a LambdaExpression object.

挽容 2024-10-26 19:50:31

仔细阅读错误消息的内容。 LambdaExpression 不是委托。这是正常的班级。阅读 http://msdn.microsoft.com/en- us/library/system.linq.expressions.lambdaexpression.aspx。因为它的名称中有 Lambda 并不意味着它与“真正的”lambda 相同。

Read carefully what errror message is saying. LambdaExpression is not a delegate. It is normal class. Read http://msdn.microsoft.com/en-us/library/system.linq.expressions.lambdaexpression.aspx. Because it has Lambda in name doesn't mean it is same as 'true' lambda.

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