为什么我会收到此“对象引用和响应不可用”错误?

发布于 2024-09-28 20:35:50 字数 1456 浏览 4 评论 0原文

这个问题中我发现了以下内容,但有两个错误我无法解决。

该错误在导致该错误的语句中被提及为 ***//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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扬花落满肩 2024-10-05 20:35:50

看起来您想要使用当前页面(_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 replacing obj with it.

恬淡成诗 2024-10-05 20:35:50

Response 属于当前设置为页面的 Response 属性的 HttpContext,并且您没有使用 Activator.CreateInstance 获取正确的上下文() 我猜。如果您使用 HttpContext.Current.Response.Write(a) 而不是 Response.Write(a),它可以工作:

HttpContext.Current.Response.Write(a)

对于标签情况,您需要:

Label lbl = (HttpContext.Current.Handler as Page).FindControl("Label1") as Label;
lbl.Text = "one i called";

这确实可以我猜你是什么意思。但你真的需要这样做吗,或者只是为了练习。

Response belongs to the current HttpContext that is set to the Page's Response property and you are not getting the right context using Activator.CreateInstance() I guess. If you use HttpContext.Current.Response.Write(a) instead of Response.Write(a), it works:

HttpContext.Current.Response.Write(a)

For the label case, you need:

Label lbl = (HttpContext.Current.Handler as Page).FindControl("Label1") as Label;
lbl.Text = "one i called";

This exactly does what you mean I guess. But do you really need to do this, or is it just for practice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文