在this question我發現了以下,但有兩個我無法解決的錯誤。爲什麼我得到這個對象引用和響應不可用錯誤?
該錯誤與聲明引起它爲***//error is***
。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
//using System.Collections;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string function_name;
function_name = "one";
caller(function_name);
}
static void caller(String function_name)
{
// Get a type from the string
Type type = typeof(_Default);
// Create an instance of that type
Object obj = Activator.CreateInstance(type);
// Retrieve the method you are looking for
MethodInfo methodInfo = type.GetMethod(function_name);
// Invoke the method on the instance we created above
methodInfo.Invoke(obj, null);
}
public void one() //function
{
string a = "\n this is the alphabet a \n";
***//error is***
////Object reference not set to an instance of an object.
////Label1.Text = "one i called";
***//error is***
/////Response is not available in this context.
//// Response.Write(a);
}// function one ends
}
這就是你的解決方案 – 2010-10-20 09:06:18
我試過你說的,但「這個」並不在函數的範圍內,你會更具體地說明在哪裏添加這個? – user287745 2010-10-20 09:16:26
以及爲什麼我將此傳遞給調用者,調用者擁有方法名稱。它與obj沒有任何關係。我不明白.. – user287745 2010-10-20 09:21:51