如何使用linq表达式将方法作为另一个方法的参数传递

nan*_*ano 5 c# linq expression func

我想创建一个在后台线程中运行另一个方法的方法.像这样的东西:

void Method1(string param)
{
    // Some Code
}

void Method2(string param)
{
    // Some Code
}

void RunInThread(AMethod m)
{
   //Run the method in a background thread
}
Run Code Online (Sandbox Code Playgroud)

Hos*_*Rad 8

如果您的方法具有返回值,请使用Func委托,否则您可以使用Action委托.例如:

void Method1(string param)
{
    // Some Code
}

void Method2(string param)
{
   // Some Code
}

void RunInThread(Action<string> m)
{
   //Run the method in a background thread
}
Run Code Online (Sandbox Code Playgroud)

然后你可以这样打电话RunInThread:

RunInThread(Method1);
RunInThread(Method2);
Run Code Online (Sandbox Code Playgroud)