且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将任何方法作为另一个函数的参数传递

更新时间:2023-02-14 15:35:04

你需要传递一个 delegate 实例;Action 可以正常工作:

You need to pass a delegate instance; Action would work fine:

internal void AFoo(string s, Action doOtherThing)
{
    if (something)
    {
        //do something
    }
    else
        doOtherThing();
}

如果 BFoo 是无参数的,它将按照您的示例中所写的那样工作:

If BFoo is parameterless it will work as written in your example:

new ClassA().AFoo("hi", BFoo);

如果它需要参数,你需要提供它们:

If it needs parameters, you'll need to supply them:

new ClassA().AFoo("hi", () => BFoo(123, true, "def"));