C# 中 SomeMethod(() => x.Something) 是什么意思
(注意代码是一个示例)
我有以下语法:
SomeMethod(() => x.Something)
表达式中第一个括号的含义是什么?
我也很好奇如何从传入的参数中获取属性名称。这可能吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是不带参数的方法的 lambda 语法。如果它采用 1 个参数,则为:
如果采用 n + 1 个参数,则为:
如果您的
SomeMethod
采用Expression>
,那么可以:It's the lambda syntax for a method that takes no parameters. If it took 1 parameter, it'd be:
If it took n + 1 arguments, then it'd be:
If your
SomeMethod
takes anExpression<Func<T>>
, then yes:()
是一个空参数列表。您正在定义一个不带参数并返回 x.Something 的匿名函数。编辑:它与
x => 不同x.Something
,因为后者需要一个参数,并且在该参数上调用 Something。在以前的版本中,x
必须存在于函数外部的某个位置,并且在x
外部调用某些东西。对于后一个版本,不必有外部 x,即使有,仍然会在函数的参数上调用 Something,而不是其他。The
()
is an empty argument list. You're defining an anonymous function that takes no arguments and returnsx.Something
.Edit: It differs from
x => x.Something
in that the latter requires an argument and Something is called on that argument. With the former versionx
has to exist somewhere outside the function and Something is called on that outsidex
. With the latter version there does not have to be an outside x and even if there is, Something is still called on the argument to the function and nothing else.这是一个 lambda 表达式。也就是说,它是创建匿名函数或委托的一种方法。
一般形式是:
如果有,
那么您已经创建了一个不带参数并返回表达式结果的函数。
C# 使用 类型推断 来确定值的类型,并捕获局部变量(就像你的“x”变量)通过词汇闭包。
It's a lambda expression. That is, it's a way to create an anonymous function or delegate.
The general form is:
If you have
then you have created a function that takes no arguments, and returns the result of the expression.
C# uses type inference to figure out what the types of the values are, and it captures local variables (like your "x" variable) by means of a lexical closure.
我假设 x 在您的方法内的某处声明,如果是,您可以将此 lambda 表达式与没有参数的委托进行比较,并返回 x.someproperty 的类型,
该类型与以下内容相同:
I assume x is declared in somewhere inside your method, if yes, you can compare this lambda expression with a delegate that has no paramaters and return the type of x.someproperty
that is the same as:
() 表示该方法不带任何参数。
例如,如果您使用 lambda 表达式分配普通事件处理程序,它将如下所示:
the () mean that this method doesn't take any parameters.
for example, if you assign a normal event handler using a lambda expression, it would look like this:
另请参阅以下两篇博客文章,它们准确讨论了您的第二个问题并提供了替代方法:
如何在 C# 中查找变量或参数名称?
如何通过 IL 从 C# Lambda 获取参数名称和参数值?(或者“如何不使用 .NET Linq 表达式来从 C# Lambda 获取参数名称和参数值?”)
See also the following two blog posts that discuss exactly your second question and provide alternative approaches:
How to Find Out Variable or Parameter Name in C#?
How to Get Parameter Name and Argument Value From C# Lambda via IL? (Or "How NOT to Use .NET Linq Expressions in Order to Get Parameter Name and Argument Value From C# Lambda?")
要获取属性的名称,您需要 SomeMethod 具有
System.Linq.Expressions.Expression>
类型的参数。然后,您可以通过表达式来确定属性名称。To get the name of the property you need SomeMethod to have an argument of the type of
System.Linq.Expressions.Expression<System.Func<object>>
. You can then go through the expression to determine the property name.