Asp.net's AutoEventWireup and reflection?

.net, .net-4.0, asp.net, autoeventwireup, c#

Solution

It seems that ASP.NET uses cache for that as @Marc said. See internal `TemplateControl.HookUpAutomaticHandlers`.

A part of this method by using 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;
      }
    }
  }
  ...

Private `GetDelegateInformation` method is responsible for creating delegates for the control. `TemplateControl._eventListCache` is a `Hashtable` which holds delegates per template control.

So, answering your question:

Does he do it for every request ?

The answer is no. ASP.NET does it once to populate this `Hashtable`, and then uses cached values.

Problem

`AutoEventWireup` uses reflection searching page methods by `page_eventName` msdn When AutoEventWireup is true, handlers are automatically bound to events at run time based on their name and signature. For each event, ASP.NET searches for a method that is named according to the pattern Page_eventname, such as Page_Load or Page_Init. Question : Does he do it for every request ? I looked in the `temporary internet files` (at `Microsoft.net` folder...) to see if he saves another file which contains explicit handler attachment - and couldn't find any .

Original source