Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将方法作为方法参数调用但不执行?_C#_.net_Methods_Delegates_Func - Fatal编程技术网

C# 将方法作为方法参数调用但不执行?

C# 将方法作为方法参数调用但不执行?,c#,.net,methods,delegates,func,C#,.net,Methods,Delegates,Func,嗨,我正在尝试实现一个方法,该方法将一个方法(事物大方案中的任何方法)作为参数。我希望此参数方法仅在调用它的方法中运行,如果传入此方法的方法具有返回值,则它仍应能够返回其值 我想测量传入的方法的性能 return Performance(GetNextPage(eEvent, false)); public static T Performance<T>(T method) { T toReturn; Stopwatch sw = Stopwatch.StartNe

嗨,我正在尝试实现一个方法,该方法将一个方法(事物大方案中的任何方法)作为参数。我希望此参数方法仅在调用它的方法中运行,如果传入此方法的方法具有返回值,则它仍应能够返回其值

我想测量传入的方法的性能

return Performance(GetNextPage(eEvent, false));

public static T Performance<T>(T method)
{
    T toReturn;
    Stopwatch sw = Stopwatch.StartNew();
    toReturn = method;
    sw.Stop();
    Debug.WriteLine(sw.Elapsed.ToString());
    return toReturn;
}
问题是
action()
返回
void
,所以我不能以我想要的方式使用它


我已经查看了
Func
,但是我不知道如何让它通过传入的
GetNextPage
方法运行我的
Performance
方法。

您需要将
Func
传递到
Performance

return Performance(GetNextPage(eEvent, false));

public static T Performance<T>(T method)
{
    T toReturn;
    Stopwatch sw = Stopwatch.StartNew();
    toReturn = method;
    sw.Stop();
    Debug.WriteLine(sw.Elapsed.ToString());
    return toReturn;
}
public static T Performance<T>(Func<T> func)
{
    T toReturn;
    Stopwatch sw = Stopwatch.StartNew();
    toReturn = func();
    sw.Stop();
    Debug.WriteLine(sw.Elapsed.ToString());
    return toReturn;
}
eEvent
false
成为闭包的一部分,因此获取并返回的只是
GetNextPage
的返回结果

return Performance(() => GetNextPage(eEvent, false));