2012-11-14 32 views
4

AutoEventWireup通过page_eventNameAsp.net的AutoEventWireup和反射?

msdn

当AutoEventWireup为true使用反射搜索页面的方法,处理程序自动绑定到在运行时根据自己的姓名和签字 事件。对于每个事件, ASP.NET搜索按照模式 Page_eventname命名的方法,例如Page_Load或Page_Init。

问:

难道他对要求做呢?

我看着在temporary internet files(在Microsoft.net文件夹...),看他是否保存它包含明确的处理程序附件另一个文件 - 和无法狼狈不堪。

+3

有趣的问题;它不会被* required *这样做,因为这个策略可以被缓存(甚至可以通过元编程来编译)。但是,我不知道它实际上*做了什么。 –

回答

4

似乎ASP.NET使用缓存作为@Marc说。见内部TemplateControl.HookUpAutomaticHandlers

通过使用dotPeek该方法的一部分:

internal void HookUpAutomaticHandlers() 
{ 
    ... 
    object obj = TemplateControl._eventListCache[(object) this.GetType()]; 
    if (obj == null) 
    { 
    lock (TemplateControl._lockObject) 
    { 
     obj = TemplateControl._eventListCache[(object) this.GetType()]; 
     if (obj == null) 
     { 
     IDictionary local_1_1 = (IDictionary) new ListDictionary(); 
     this.GetDelegateInformation(local_1_1); 
     obj = local_1_1.Count != 0 ? (object) local_1_1 : TemplateControl._emptyEventSingleton; 
     TemplateControl._eventListCache[(object) this.GetType()] = obj; 
     } 
    } 
    } 
    ... 

私人GetDelegateInformation方法负责对控制创建代表。 TemplateControl._eventListCache是一个Hashtable它保存每个模板控件的代表。

所以,回答你的问题:

他是否为每个请求做呢?

答案是否定的。 ASP.NET一次填充这个Hashtable,然后使用缓存的值。

+1

你的意思是静态字段'private static Hashtable _eventListCache; '...不缓存.... :-) –

+1

@罗伊纳米尔:谢谢,纠正我的答案有点:) – Alex