Arn*_*sen 8 lambda expression method-group c#-3.0
我试图弄清楚是否有一个简单的语法将方法组转换为表达式.使用lambdas似乎很容易,但它并没有转化为方法:
特定
public delegate int FuncIntInt(int x);
Run Code Online (Sandbox Code Playgroud)
以下所有内容均有效:
Func<int, int> func1 = x => x;
FuncIntInt del1 = x => x;
Expression<Func<int, int>> funcExpr1 = x => x;
Expression<FuncIntInt> delExpr1 = x => x;
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试使用实例方法,它会在表达式中分解:
Foo foo = new Foo();
Func<int, int> func2 = foo.AFuncIntInt;
FuncIntInt del2 = foo.AFuncIntInt;
Expression<Func<int, int>> funcExpr2 = foo.AFuncIntInt; // does not compile
Expression<FuncIntInt> delExpr2 = foo.AFuncIntInt; //does not compile
Run Code Online (Sandbox Code Playgroud)
最后两个都无法使用"无法将方法组'AFuncIntInt'转换为非委托类型'System.Linq.Expressions.Expression <...>'进行编译.您是否打算调用该方法?"
那么在表达式中捕获方法组有一个很好的语法吗?
谢谢,阿恩
这个怎么样?
Expression<Func<int, int>> funcExpr2 = (pArg) => foo.AFuncIntInt(pArg);
Expression<FuncIntInt> delExpr2 = (pArg) => foo.AFuncIntInt(pArg);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6013 次 |
| 最近记录: |