2010-10-05 96 views
6

下面我有一个类,C#:System.Reflection.MethodInfo原因:(对象不匹配目标类型)

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

而且我通过以下System.Reflection.MethodInfo打电话,但它造成TargetException:对象与目标类型不匹配。

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的参数改为object:foo(object s){},但它没有帮助。

回答

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

说法是不对的,它指的是你的Page类。改为传递instance_class。

+0

工作很棒~~你救了我,男人〜 – Cheung 2010-10-05 03:59:32

相关问题