C# 中 SomeMethod(() => x.Something) 是什么意思

发布于 2024-08-04 03:38:19 字数 163 浏览 5 评论 0 原文

(注意代码是一个示例)

我有以下语法:

SomeMethod(() => x.Something) 

表达式中第一个括号的含义是什么?

我也很好奇如何从传入的参数中获取属性名称。这可能吗?

(Note the code is an example)

I have the following syntax:

SomeMethod(() => x.Something) 

What do the first brackets mean in the expression?

I'm also curious how you can get the property name from argument that is being passed in. Is this posssible?

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

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

发布评论

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

评论(7

如痴如狂 2024-08-11 03:38:19

表达式中第一个括号的含义是什么?

这是不带参数的方法的 lambda 语法。如果它采用 1 个参数,则为:

SomeMethod(x => x.Something);

如果采用 n + 1 个参数,则为:

SomeMethod((x, y, ...) => x.Something);

我也很好奇如何从传入的参数中获取属性名称。这可能吗?

如果您的 SomeMethod 采用 Expression>,那么可以:

void SomeMethod<T>(Expression<Func<T>> e) {
    MemberExpression op = (MemberExpression)e.Body;
    Console.WriteLine(op.Member.Name);
}

What do the first brackets mean in the expression?

It's the lambda syntax for a method that takes no parameters. If it took 1 parameter, it'd be:

SomeMethod(x => x.Something);

If it took n + 1 arguments, then it'd be:

SomeMethod((x, y, ...) => x.Something);

I'm also curious how you can get the property name from argument that is being passed in. Is this possible?

If your SomeMethod takes an Expression<Func<T>>, then yes:

void SomeMethod<T>(Expression<Func<T>> e) {
    MemberExpression op = (MemberExpression)e.Body;
    Console.WriteLine(op.Member.Name);
}
从此见与不见 2024-08-11 03:38:19

() 是一个空参数列表。您正在定义一个不带参数并返回 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 returns x.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 version x has to exist somewhere outside the function and Something is called on that outside x. 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.

蓬勃野心 2024-08-11 03:38:19

这是一个 lambda 表达式。也就是说,它是创建匿名函数或委托的一种方法。

一般形式是:

(input parameters) => expression

如果有,

() => expression

那么您已经创建了一个不带参数并返回表达式结果的函数。

C# 使用 类型推断 来确定值的类型,并捕获局部变量(就像你的“x”变量)通过词汇闭包

It's a lambda expression. That is, it's a way to create an anonymous function or delegate.

The general form is:

(input parameters) => expression

If you have

() => expression

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.

递刀给你 2024-08-11 03:38:19

我假设 x 在您的方法内的某处声明,如果是,您可以将此 lambda 表达式与没有参数的委托进行比较,并返回 x.someproperty 的类型,

delegate{
 return x.someproperty;
}

该类型与以下内容相同:

() => 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

delegate{
 return x.someproperty;
}

that is the same as:

() => x.someproperty
网名女生简单气质 2024-08-11 03:38:19

() 表示该方法不带任何参数。
例如,如果您使用 lambda 表达式分配普通事件处理程序,它将如下所示:

someButton.Click += (s, e) => DoSomething();

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:

someButton.Click += (s, e) => DoSomething();
如梦初醒的夏天 2024-08-11 03:38:19

另请参阅以下两篇博客文章,它们准确讨论了您的第二个问题并提供了替代方法:

如何在 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?")

雨后咖啡店 2024-08-11 03:38:19

要获取属性的名称,您需要 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.

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