1
是否有任何方式使用字符串初始化委托?即你不会知道需要在运行时调用的函数的名字吗?或者我猜这样做有更好的方法吗?使用字符串初始化委托使用字符串
delegate void TestDelegate(myClass obj);
void TestFunction()
{
TestDelegate td = new TestDelegate(myFuncName); // works
TestDelegate td = new TestDelegate("myFuncName"); // doesn't work
}
更新
是的代码是我目前有哪些工作不
class Program
{
static void Main(string[] args)
{
Bish b = new Bish();
b.MMM();
Console.Read();
}
}
class Bish
{
delegate void TestDelegate();
public void MMM()
{
TestDelegate tDel = (TestDelegate)this.GetType().GetMethod("PrintMe").CreateDelegate(typeof(TestDelegate));
tDel.Invoke();
}
void PrintMe()
{
Console.WriteLine("blah");
}
}
我得到一个系统空引用异常? – mHelpMe
@mHelpMe方法是静态的吗? – IllidanS4
@ IllidanS4我刚刚在 – mHelpMe