簡體   English   中英

C#:System.Reflection.MethodInfo原因:(對象與目標類型不匹配)

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

我下面有一節課,

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而不是。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM