2010-10-05 18 views
6
aşağıda bir sınıf var

,C#: System.Reflection.MethodInfo neden: (Nesne eşleşmiyor hedef türü)

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

Ve aşağıda System.Reflection.MethodInfo aracılığıyla diyorlar, ama TargetException neden: Nesne hedef türüyle eşleşmiyor.

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

Herhangi bir fikir? foo (object s) {} gibi foo (object s) {} gibi nesne için değiştirmeyi deneyin ama yardımcı olmuyor. bu argüman yanlıştır

cevap

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

, sizin Sayfa sınıfını ifade eder. Bunun yerine instance_class dosyasını geçirin.

+0

Harika çalış ~ ~ beni kurtardın, adamım ~ – Cheung

İlgili konular