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

Che*_*ung 6 c#

我下面有一节课,

namespace PocketWeb.AppClass
{
    public class ApiBase
    {
        public string foo(string s)
        {
            return s;
        }
    }
 }
Run Code Online (Sandbox Code Playgroud)

我通过下面的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
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Han*_*ant 17

   Response.Write(theMethod.Invoke(this, obj));
Run Code Online (Sandbox Code Playgroud)

这种说法是不对的,它指的是你的Page类.传递instance_class而不是.