c#反射重载方法,发现不明确的匹配

GetMethod(string name)

在反射重载方法时,如果调用此重载方法,会产生 发现不明确的匹配 的错误。

解决方案如下:

GetMethod("MethodName", new Type [] { typeof(参数类型)});

其中type数组中的项的个数是由要调用的方法的参数个数来决定的。

如果无参数,则new Type[]{},使Type数组中的项个数为0

 public int IntAdd(int a, int b)
        {
            return a + b;
        }

        public string Show()
        {
            return "Nothing";
        }

        public string Show(string str)
        {
            return str;
        }
 MethodInfo ss = myType.GetMethod("Show", new Type[] { typeof(string) });
            Console.WriteLine(ss.Invoke(obj, new string[] { "ok" }));

如果获得没有参数的SHOW,就为空就可以了!