为什么我会收到此“对象引用和响应不可用”错误?
在这个问题中我发现了以下内容,但有两个错误我无法解决。
该错误在导致该错误的语句中被提及为 ***//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
}
In this question I found the following, but there are two errors which I can not solve.
The error is mentioned with the statement causing it as ***//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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您想要使用当前页面(_default 的实例)而不是创建新页面。
尝试将
this
传递给调用者,并用它替换obj
。It looks like you want to work with the current page (instance of a _default) instead of creating a new one.
Try passing
this
into caller, and replacingobj
with it.Response
属于当前设置为页面的Response
属性的HttpContext
,并且您没有使用Activator.CreateInstance 获取正确的上下文()
我猜。如果您使用HttpContext.Current.Response.Write(a)
而不是Response.Write(a)
,它可以工作:对于标签情况,您需要:
这确实可以我猜你是什么意思。但你真的需要这样做吗,或者只是为了练习。
Response
belongs to the currentHttpContext
that is set to the Page'sResponse
property and you are not getting the right context usingActivator.CreateInstance()
I guess. If you useHttpContext.Current.Response.Write(a)
instead ofResponse.Write(a)
, it works:For the label case, you need:
This exactly does what you mean I guess. But do you really need to do this, or is it just for practice.