C#:System.Reflection.MethodInfo原因:(对象与目标类型不匹配)

我下面有一节课,

namespace PocketWeb.AppClass { public class ApiBase { public string foo(string s) { return s; } } } 

我通过下面的System.Reflection.MethodInfo调用,但它导致TargetException:Object与目标类型不匹配。

  protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { var instance_class = Activator.CreateInstance(Type.GetType("PocketWeb.AppClass.ApiBase")); Type instance_method = instance_class.GetType(); System.Reflection.MethodInfo theMethod = instance_method.GetMethod("foo"); object[] obj = new object[] { "hello" }; Response.Write(theMethod.Invoke(this, obj)); //<---Error } } 

那么任何想法? 我尝试将foo的参数更改为对象,如:foo(object s){},但它没有帮助。

  Response.Write(theMethod.Invoke(this, obj)); 

这个参数是错误的,它指的是你的Page类。 传递instance_class而不是。