2
我有这段代码,它接受一个没有参数的函数,并返回它的运行时。传递一个具有多个参数的函数作为参数
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的返回值,并获得最终的秒表。 任何帮助,非常感谢!
谢谢你,但这种技术,我能得到日e返回值? –
如果你对返回值感兴趣,那么你应该通过'Func'代替。我已经编辑了有关它的信息的答案。 –
carlosfigueira
非常感谢! –