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

时间:2012-06-14 04:00:46

标签: methods delegates

在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);

...

0 个答案:

没有答案
相关问题