为什么我会收到此对象引用和响应不可用错误? [英] Why am I getting this Object Reference and Response not Available error?

查看:51
本文介绍了为什么我会收到此对象引用和响应不可用错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In this question I found the following, but there are two errors which I can not solve.

错误与导致它的语句一起被提及为***//error is***.

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
}

推荐答案

看起来您想使用当前页面(_default 的实例)而不是创建新页面.

It looks like you want to work with the current page (instance of a _default) instead of creating a new one.

尝试将 this 传递给调用者,并用它替换 obj.

Try passing this into caller, and replacing obj with it.

这篇关于为什么我会收到此对象引用和响应不可用错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆