Given a method DoSomething
that takes a (parameterless) function and handles it in some way. Is there a better way to create the "overloads" for functions with parameters than the snippet below?
public static TResult DoSomething<TResult>(Func<TResult> func)
{
//call func() and do something else
}
public static TResult DoSomething<T0, TResult>(
Func<T0, TResult> func,
T0 arg0)
{
return DoSomething(() => func(arg0));
}
public static TResult DoSomething<T0, T1, TResult>(
Func<T0, T1, TResult> func,
T0 arg0, T1 arg1)
{
return DoSomething(arg => func(arg, arg1), arg0);
}
public static TResult DoSomething<T0, T1, T2, TResult>(
Func<T0, T1, T2, TResult> func,
T0 arg0, T1 arg1, T2 arg2)
{
return DoSomething(arg => func(arg, arg1, arg2), arg0);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…