本文介绍了SomeMethod(()=> x.Something)在C#中是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(请注意代码是示例)

我有以下语法:

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?

推荐答案

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

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

SomeMethod(x => x.Something);

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

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

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

如果您的SomeMethod使用Expression<Func<T>>,则可以:

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

这篇关于SomeMethod(()=&gt; x.Something)在C#中是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 16:26