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

问题描述:

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

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.