我有这段代码,它接受一个没有参数的函数,并返回其运行时间。
public static Stopwatch With_StopWatch(Action action)
{
var stopwatch = Stopwatch.StartNew();
action();
stopwatch.Stop();
return stopwatch;
}
我希望将其转换为接受带参数的非 void 函数。我听说过 Func<> 委托,但我不知道如何使用它。我需要类似于以下内容(非常伪代码):
public T measureThis(ref Stopwatch sw, TheFunctionToMeasure(parameterA,parameterB))
{
sw.Start(); // start stopwatch
T returnVal = TheFunctionToMeasure(A,B); // call the func with the parameters
stopwatch.Stop(); // stop sw
return returnVal; // return my func's return val
}
所以我需要获取传递函数的返回值,并在最后获取秒表。 非常感谢您的帮助!
Func<T>
。我已经编辑了答案并提供了相关信息。 - carlosfigueira