是否有用于在各种类型的 Func 委托之间进行转换的内置方法?也就是说,假设您需要一个 Func,但您有一个 Func(并且您有应该为 T 参数传入的值)。例如:
static TREsult Foo<TResult>(Func<TResult> f)
{
// ...
TResult result = f();
// ...
return result;
}
static int MyFunc(int i)
{
return i;
}
void CallFoo()
{
Func<int> func = ConvertFunc(MyFunc, 1); // Does this family of methods exist?
int j = Foo(func);
}
我自己写了,像这样:
static Func<TResult> ConvertFunc<T, TResult>(Func<T, TResult> f1, T t)
{
return () => f1(t);
}
static Func<TResult> ConvertFunc<T1, T2, TResult>(Func<T1, T2, TResult> f2, T1 t1, T2 t2)
{
return () => f2(t1, t2);
}
// etc.
但我想知道是否存在这样的一系列方法(或者即使有更好的方法来做到这一点)。
本质上,我这样做是为了在方法中有一些样板代码,然后是函数调用(函数中的数量和类型会有所不同,但返回类型相同),然后是更多样板代码车牌代码。
欢迎大家发表意见!谢谢。