我试图写一个函数,函数作为它的一个参数 - 我以前做过很多次的任务。这工作得很好:功能可以运行一个方向,而不是其他
int RunFunction(Func<int,int> f, int input) {
return f(input);
}
int Double(int x) {
return x*2;
}
// somewhere else in code
RunFunction(Double,5);
然而,这不起作用:
public static class FunctionyStuff {
public static int RunFunction(this Func<int,int> f, int input) {
return f(input);
}
}
// somewhere else in code
Double.RunFunction(5);
任何想法,为什么第一个作品,第二个不?
也许你也应该解释为什么* *它不发生的扩展方法和lambda表达式。这是因为他们没有特定的委托类型。 'Func键'和'假设可能MyIntIntDelegate'具有相同的签名,但将是不同的类型,所以编译器可能不知道要转换成哪一个。 –
Timwi
2012-03-18 00:18:53
所以如果我得到这个权利,在第一个版本中有一个从'Method'到'Func'的隐式转换? – Joe 2012-03-18 00:19:56
@Timwi:我正在那里... – 2012-03-18 00:24:11