当然可以使用Func<T1, T2, TResult> 委托:
public String myMethod(
Func<string, string, string> f1,
Func<string, string, string> f2)
{
//code
}
这个委托定义了一个函数,它接受两个字符串参数并返回一个字符串。它有许多表亲来定义采用不同数量参数的函数。要使用其他方法调用myMethod,只需传入方法名称即可,例如:
public String doSomething(String s1, String s2) { ... }
public String doSomethingElse(String s1, String s2) { ... }
public String myMethod(
Func<string, string, string> f1,
Func<string, string, string> f2)
{
//code
string result1 = f1("foo", "bar");
string result2 = f2("bar", "baz");
//code
}
...
myMethod(doSomething, doSomethingElse);
当然,如果f2的参数和返回类型不完全相同,你可能需要相应地调整方法签名。