Methods 将任何方法(不仅仅是Action或Func)作为参数传递

Methods 将任何方法(不仅仅是Action或Func)作为参数传递,methods,delegates,Methods,Delegates,在C语言中,我想知道如何传递一个方法,任何方法,作为参数 我知道Action和Func,但它们受到方法的限制,要么在前者中无效,要么在后者中需要返回值。我想包括对这两种情况的支持,如果可能的话,我可能有一个param方法数组 我假设其中涉及到了委托的使用,但我不确定在使用和不使用lambdas时是如何使用的。谢谢 类似于 public void DoSomething(int num) {...} public void DoSomethingElse() {...} public int

在C语言中,我想知道如何传递一个方法,任何方法,作为参数

我知道Action和Func,但它们受到方法的限制,要么在前者中无效,要么在后者中需要返回值。我想包括对这两种情况的支持,如果可能的话,我可能有一个param方法数组

我假设其中涉及到了委托的使用,但我不确定在使用和不使用lambdas时是如何使用的。谢谢

类似于

public void DoSomething(int num)
{...}

public void DoSomethingElse()
{...}

public int ReturnSomething(string str, int num)
{...}

public void RunMethods(params object[] methods) // (params delegate[] methods)?
{
    foreach(var method in methods)
    {
        ...determine whether its void or not and run.
    }
}

RunMethod(DoSomething, ReturnSomething, DoSomethingElse);

...

无法传递方法-它们不是一级构造。但是,Method1Method2将起作用,因为如果可能存在[n隐式]转换,编译器将提升方法组Method2。我向一位代表想了想,但魔法的细节让我不知所措。至于在过去支持Func和Action,我制造了一个过载;动作中的重载只是包装Func重载,将动作放入存根函数中,并放弃返回void。我不确定这是否是理想的,但它已经为我工作了。。。另一个选项是只接受Func并返回defaultR。。。