2009-12-22 78 views

回答

11

没什么特别需要调用这种方法:

object o = new DummyClass(); 

MethodInfo method = typeof(DummyClass).GetMethod("Greetings"); 
string[] a = (string[])method.Invoke(o, null); 
+0

谢谢,var关键字可以减少到 var a = method.Invoke(0,null); – 2009-12-22 05:35:10

2

以下是你需要使用反射来调用一个方法的代码(请IND - 该MethodInfo.Invoke法”返回类型是“对象“):

DummyClass dummy = new DummyClass(); 

    MethodInfo theMethod = dummy.GetType().GetMethod("Greetings", BindingFlags.Public | BindingFlags.Instance); 
    if (theMethod != null) 
    { 
     string[] ret = (string[])theMethod.Invoke(dummy, null); 
    }