我正在尝试找出是否存在将方法组转换为表达式的简单语法。使用lambda似乎很容易,但对于方法来说就不是这样:
假设有以下代码:
public delegate int FuncIntInt(int x);
以下所有内容都是有效的:
Func<int, int> func1 = x => x;
FuncIntInt del1 = x => x;
Expression<Func<int, int>> funcExpr1 = x => x;
Expression<FuncIntInt> delExpr1 = x => x;
但是如果我尝试使用实例方法进行相同的操作,它会在表达式处出现错误:
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
最后两个示例都无法编译,出现“无法将方法组'AFuncIntInt'转换为非委托类型'System.Linq.Expressions.Expression<...>'。 您打算调用该方法吗?”的错误消息。
那么,有没有一种良好的语法可以在表达式中捕获方法组呢?
谢谢, arne