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

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类型。

其他回答

如果传递的方法需要接受一个参数并返回一个值,那么Func是最好的方法。这里有一个例子。

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

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

public bool RunTheMethod(Func<string, int> myMethodName)
{
    // Do stuff
    int i = myMethodName("My String");
    Console.WriteLine(i); // This is just in place of the "Do more stuff"
    return true;
}

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

阅读此处的文档

但是,如果作为参数传递的方法没有返回任何内容,也可以使用Action。对于传递的方法,它最多支持16个参数。这里有一个例子。

public int MethodToBeCalled(string name, int age)
{
    Console.WriteLine(name + "'s age is" + age);
}

public bool RunTheMethod(Action<string, int> myMethodName)
{
    // Do stuff
    myMethodName("bob", 32); // Expected output: "bob's age is 32"
    return true;
}

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

阅读此处的文档

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

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

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

var returnValue = Runner(() => GetUser(99));
class PersonDB
{
  string[] list = { "John", "Sam", "Dave" };
  public void Process(ProcessPersonDelegate f)
  {
    foreach(string s in list) f(s);
  }
}

第二个类是Client,它将使用存储类。它有一个创建PersonDB实例的Main方法,并使用Client类中定义的方法调用该对象的Process方法。

class Client
{
  static void Main()
  {
    PersonDB p = new PersonDB();
    p.Process(PrintName);
  }
  static void PrintName(string name)
  {
    System.Console.WriteLine(name);
  }
}

如果要将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");
}

下面是一个没有参数的示例:http://en.csharp-online.net/CSharp_FAQ:_How_call_a_method_using_a_name_string

参数为:http://www.daniweb.com/forums/thread98148.html#

您基本上传递了一个对象数组以及方法名。然后将两者与Invoke方法一起使用。

params对象[]参数