实现Asp.Net里的Page页面上的函数可有可无(Page_Init等)
在Asp.net中大家都知道,在一个aspx的代码页中可以定义Page_Init和Page_UnLoad等页面函数,定义了它就会在Page对象中调用事件的时候执行相应的Page函数,没定义就不会执行也不会报错,这是怎么实现的呢,这里我提供一种使用反射的方法来实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ReflectionDelegate
{
class Program
{
class Page
{
public delegate void DelMethod();
public event DelMethod OnMethod;//此事件就如同ASP.NET的Page对象里面的OnInit事件
public int i = 0;
public void ArouseMethodEvent()
{
Type t = this.GetType();//在MyPage中调用this.GetType()返回的是实际上是MyPage的Type,因为ArouseMethodEvent是由MyPage的对象调用的,所以这里的this也就是MyPage的对象
MethodInfo mi = t.GetMethod("method", BindingFlags.NonPublic | BindingFlags.Instance);//反射是否定义了method函数
if (mi != null)
{
Delegate dtemp = Delegate.CreateDelegate(typeof(Page.DelMethod), this, mi, true);//创建Page.DelMethod委托实例,并赋给dtemp,其中this表示mi所封装函数method的调用对象,即指定在使用委托实例或事件调用函数method的时候,method中的this代表的是哪个对象
this.OnMethod += dtemp as Page.DelMethod;//因为所有自定义委托都继承自Delgate,所以可以将dtemp as Page.DelMethod
if (this.OnMethod != null)
this.OnMethod();
}
}
}
class MyPage : Page
{
protected void method()//此函数就如同ASP.NET的Page对象里面的Page_Init等函数,可有可无
{
Console.WriteLine("被事件调用");
Console.WriteLine("其中i=" + this.i.ToString());//这里的this就是上面调用Delegate.CreateDelegate传入的第二个参数的this
}
}
static void Main(string[] args)
{
MyPage page = new MyPage();
page.i = 100;
page.ArouseMethodEvent();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ReflectionDelegate
{
class Program
{
class Page
{
public delegate void DelMethod();
public event DelMethod OnMethod;//此事件就如同ASP.NET的Page对象里面的OnInit事件
public int i = 0;
public void ArouseMethodEvent()
{
Type t = this.GetType();//在MyPage中调用this.GetType()返回的是实际上是MyPage的Type,因为ArouseMethodEvent是由MyPage的对象调用的,所以这里的this也就是MyPage的对象
MethodInfo mi = t.GetMethod("method", BindingFlags.NonPublic | BindingFlags.Instance);//反射是否定义了method函数
if (mi != null)
{
Delegate dtemp = Delegate.CreateDelegate(typeof(Page.DelMethod), this, mi, true);//创建Page.DelMethod委托实例,并赋给dtemp,其中this表示mi所封装函数method的调用对象,即指定在使用委托实例或事件调用函数method的时候,method中的this代表的是哪个对象
this.OnMethod += dtemp as Page.DelMethod;//因为所有自定义委托都继承自Delgate,所以可以将dtemp as Page.DelMethod
if (this.OnMethod != null)
this.OnMethod();
}
}
}
class MyPage : Page
{
protected void method()//此函数就如同ASP.NET的Page对象里面的Page_Init等函数,可有可无
{
Console.WriteLine("被事件调用");
Console.WriteLine("其中i=" + this.i.ToString());//这里的this就是上面调用Delegate.CreateDelegate传入的第二个参数的this
}
}
static void Main(string[] args)
{
MyPage page = new MyPage();
page.i = 100;
page.ArouseMethodEvent();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架