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

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代码,因为我需要定义参数。


当前回答

您需要使用代理。在这种情况下,所有方法都接受一个字符串参数并返回一个int-这最简单地由Func<string,int>delegate1表示。因此,您的代码可以通过以下简单的更改变得正确:

public bool RunTheMethod(Func<string, int> myMethodName)
{
    // ... do stuff
    int i = myMethodName("My String");
    // ... do more stuff
    return true;
}

诚然,代表们拥有比这多得多的权力。例如,使用C#,您可以从lambda表达式创建一个委托,因此您可以这样调用方法:

RunTheMethod(x => x.Length);

这将创建如下匿名函数:

// The <> in the name make it "unspeakable" - you can't refer to this method directly
// in your own code.
private static int <>_HiddenMethod_<>(string x)
{
    return x.Length;
}

然后将该委托传递给RunTheMethod方法。

您可以将委托用于事件订阅、异步执行、回调-所有类型的事情。它非常值得一读,特别是如果你想使用LINQ。我有一篇文章,主要是关于代表和事件之间的差异,但无论如何你可能会发现它很有用。


1这只是基于框架中的泛型Func<T,TResult>委托类型;您可以很容易地声明自己的:

public delegate int MyDelegateType(string value)

然后将参数设置为MyDelegateType类型。

其他回答

我不知道谁可能需要这个,但如果您不确定如何使用委托发送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."));
}

可以将.NET 3.5中的Func委托用作RunTheMethod方法中的参数。Func委托允许您指定一个方法,该方法接受特定类型的多个参数,并返回特定类型的单个参数。下面是一个应该有效的示例:

public class Class1
{
    public int Method1(string input)
    {
        //... do something
        return 0;
    }

    public int Method2(string input)
    {
        //... do something different
        return 1;
    }

    public bool RunTheMethod(Func<string, int> myMethodName)
    {
        //... do stuff
        int i = myMethodName("My String");
        //... do more stuff
        return true;
    }

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

您应该使用一个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);
}

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

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

这里需要创建一个代理。

父.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;
    }

根据OP的示例:

 public static int Method1(string mystring)
 {
      return 1;
 }

 public static int Method2(string mystring)
 {
     return 2;
 }

你可以试试行动代表!然后使用

 public bool RunTheMethod(Action myMethodName)
 {
      myMethodName();   // note: the return value got discarded
      return true;
 }

RunTheMethod(() => Method1("MyString1"));

public static object InvokeMethod(Delegate method, params object[] args)
{
     return method.DynamicInvoke(args);
}

然后简单地调用方法

Console.WriteLine(InvokeMethod(new Func<string,int>(Method1), "MyString1"));

Console.WriteLine(InvokeMethod(new Func<string, int>(Method2), "MyString2"));