我有几个方法都具有相同的参数类型和返回值,但名称和块不同。我想将要运行的方法的名称传递给另一个将调用传递的方法的方法。

public int Method1(string)
{
    // Do something
    return myInt;
}

public int Method2(string)
{
    // Do something different
    return myInt;
}

public bool RunTheMethod([Method Name passed in here] myMethodName)
{
    // Do stuff
    int i = myMethodName("My String");
    // Do more stuff
    return true;
}

public bool Test()
{
    return RunTheMethod(Method1);
}

这段代码不起作用,但这是我正在尝试做的。我不明白的是如何编写RunTheMethod代码,因为我需要定义参数。


当前回答

您应该使用一个Func<string,int>委托,它表示一个接受字符串参数并返回int值的函数:

public bool RunTheMethod(Func<string, int> myMethod)
{
    // Do stuff
    myMethod.Invoke("My String");
    // Do stuff
    return true;
}

然后以这种方式调用它:

public bool Test()
{
    return RunTheMethod(Method1);
}

其他回答

解决方案涉及委托,委托用于存储要调用的方法。定义一个以委托为参数的方法,

public static T Runner<T>(Func<T> funcToRun)
{
    // Do stuff before running function as normal
    return funcToRun();
}

然后在调用站点上传递委托:

var returnValue = Runner(() => GetUser(99));

虽然公认的答案是绝对正确的,但我想提供一个额外的方法。

我在为一个类似的问题寻找解决方案后,最终来到了这里。我正在构建一个插件驱动的框架,作为它的一部分,我希望人们能够将菜单项添加到应用程序菜单中,而不暴露实际的菜单对象,因为该框架可能部署在其他没有菜单UI对象的平台上。添加菜单的一般信息很容易,但让插件开发人员有足够的自由在单击菜单时创建回调是一件痛苦的事。直到我意识到我正在尝试重新发明轮子和普通菜单调用,并从事件中触发回调!

所以,这个解决方案,虽然听起来很简单,但直到现在,我都没有想到。

只需为每个当前方法创建单独的类(如果必须的话,从基继承),然后为每个方法添加一个事件处理程序。

如果要将Method作为参数传递,请使用:

using System;

public void Method1()
{
    CallingMethod(CalledMethod);
}

public void CallingMethod(Action method)
{
    method();   // This will call the method that has been passed as parameter
}

public void CalledMethod()
{
    Console.WriteLine("This method is called by passing it as a parameter");
}

我不知道谁可能需要这个,但如果您不确定如何使用委托发送lambda,当使用委托的函数不需要在其中插入任何参数时,您只需要返回值。

因此,您也可以这样做:

public int DoStuff(string stuff)
{
    Console.WriteLine(stuff);
}

public static bool MethodWithDelegate(Func<int> delegate)
{
    ///do stuff
    int i = delegate();
    return i!=0;
}

public static void Main(String[] args)
{
    var answer = MethodWithDelegate(()=> DoStuff("On This random string that the MethodWithDelegate doesn't know about."));
}

下面是一个示例,它可以帮助您更好地理解如何将函数作为参数传递。

假设您有父页面,并且希望打开子弹出窗口。在父页面中有一个文本框,应该根据子弹出文本框填充。

这里需要创建一个代理。

父.cs//代表声明公共委托void FillName(String FirstName);

现在创建一个函数,该函数将填充文本框,函数应映射委托

//parameters
public void Getname(String ThisName)
{
     txtname.Text=ThisName;
}

现在单击按钮,您需要打开一个Child弹出窗口。

  private void button1_Click(object sender, RoutedEventArgs e)
  {
        ChildPopUp p = new ChildPopUp (Getname) //pass function name in its constructor

         p.Show();

    }

在ChildPopUp构造函数中,您需要创建父//页的“委托类型”参数

儿童PopUp.cs

    public  Parent.FillName obj;
    public PopUp(Parent.FillName objTMP)//parameter as deligate type
    {
        obj = objTMP;
        InitializeComponent();
    }



   private void OKButton_Click(object sender, RoutedEventArgs e)
    {


        obj(txtFirstName.Text); 
        // Getname() function will call automatically here
        this.DialogResult = true;
    }