如何使用类静态方法获取对象发送者

时间:2018-06-24 14:30:13

标签: c# asp.net

这是我的代码窃听器,用于打印消息

public partial class Default2 : System.Web.UI.Page
{
    public Default2()
    {
        DebugManage.ShowMessage(this, "hello");
    }
}

public class DebugManager
{
    static public void ShowMessage(System.Web.UI.Page handle, string message)
    {
        string script = "alert(\"MESSAGE!\");".Replace("MESSAGE", message);
        ScriptManager.RegisterStartupScript(handle, handle.GetType(), "ServerControlScript", script, true);
    }
}

是否可以更改下面熟悉的代码

public partial class Default2 : System.Web.UI.Page
{
    public Default2()
    {
        DebugManage.ShowMessage("hello");
        // no need to specify windows handle
    }
}

public class DebugManager
{
    // how to get object sender automaic
    static public void ShowMessage(object sender, string message)
    {
        string script = "alert(\"MESSAGE!\");".Replace("MESSAGE", message);
        ScriptManager.RegisterStartupScript(handle, handle.GetType(), "ServerControlScript", script, true);
    }
}

我知道像

这样的班级是否新来的
DebugManager mgr = new DebugManager(handle)

可以解决此问题。我只是很好奇,是否可以通过静态方法来做到这一点。

0 个答案:

没有答案