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

GetMethod(string name)数组

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

解决方案以下:code

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

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

若是无参数,则new Type[]{},使Type数组中的项个数为0it

 

 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,就为空就能够了!class

相关文章
相关标签/搜索