开发者

C#:System.Reflection.MethodInfo cause : (Object does not match target type)

I have a class below ,

namespace PocketWeb.Ap开发者_高级运维pClass
{
    public class ApiBase
    {
        public string foo(string s)
        {
            return s;
        }
    }
 }

And i call via System.Reflection.MethodInfo below, but it cause TargetException :Object does not match target type.

 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
    }
}

So any idea? i have try change the foo's parameter to object like : foo(object s) { } ,but it not help.


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

The this argument is wrong, it refers to your Page class. Pass instance_class instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜