2012-11-14 31 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