Global.asax event handling internals (part 2 of 3)

// HttpApplication class, from which Global is derived.
private void ReflectOnApplicationType()
{
      // Enumerates all Global and it’s wrapper methods..
      ArrayList list1 = new ArrayList();
      MethodInfo[] infoArray1 = this._theApplicationType.GetMethods(BindingFlags.NonPublic | (BindingFlags.Public | (BindingFlags.Static | BindingFlags.Instance)));
      MethodInfo[] infoArray2 = infoArray1;
      int num2 = 0;
      while (num2 < infoArray2.Length)
      {
            MethodInfo info1 = infoArray2[num2];
            // If the method’s name looks like an event handler, adds it to the list.
            if (this.ReflectOnMethodInfoIfItLooksLikeEventHandler(info1))
            {
                  list1.Add(info1);
            }
            num2++;
      }
      Type type1 = this._theApplicationType.BaseType;
      if ((type1 != null) && (type1 != typeof(HttpApplication)))
      {
            infoArray1 = type1.GetMethods(BindingFlags.NonPublic | (BindingFlags.Static | BindingFlags.Instance));
            infoArray2 = infoArray1;
            for (num2 = 0; num2 < infoArray2.Length; num2++)
            {
                  MethodInfo info2 = infoArray2[num2];
                  if (info2.IsPrivate && this.ReflectOnMethodInfoIfItLooksLikeEventHandler(info2))
                  {
                        list1.Add(info2);
                  }
            }
      }
      // Stores the list of methods for further use.
      this._eventHandlerMethods = new MethodInfo[list1.Count];
      for (int num1 = 0; num1 < this._eventHandlerMethods.Length; num1++)
      {
            this._eventHandlerMethods[num1] = (MethodInfo) list1[num1];
      }
}

 

//Também na classe HttpApplicaton

private bool ReflectOnMethodInfoIfItLooksLikeEventHandler(MethodInfo m)
{
      ParameterInfo[] infoArray1;
      string text1;
      if (m.ReturnType == typeof(void))
      {
            infoArray1 = m.GetParameters();
            switch (infoArray1.Length)
            {
                  case 0:
                  {
                        goto Label_007A;
                  }
                  case 2:
                  {
                        // The event handler’s traditional signature in .NET: object, EventArgs
                        if (infoArray1[0].ParameterType != typeof(object))
                        {
                              return false;
                        }
                        if ((infoArray1[1].ParameterType != typeof(EventArgs)) && !infoArray1[1].ParameterType.IsSubclassOf(typeof(EventArgs)))
                        {
                              return false;
                        }
                        goto Label_007A;
                  }
            }
      }
      // If it doesn’t have 0 or 2 parameters, the method is not an event handler.
      return false;
Label_007A:
      text1 = m.Name;
      int num1 = text1.IndexOf('_');
      if ((num1 <= 0) || (num1 > (text1.Length - 1)))
      {
            // “_” has to be within the name but it cannot be neither the first nor the last character.
            return false;
      }
      // Special cases that won’t be treated in HookupEventHandlersForAppplicationAndModules
      if ((string.Compare(text1, "Application_OnStart", true, CultureInfo.InvariantCulture) == 0) || (string.Compare(text1, "Application_Start", true, CultureInfo.InvariantCulture) == 0))
      {
            this._onStartMethod = m;
            this._onStartParamCount = infoArray1.Length;
      }
      else if ((string.Compare(text1, "Application_OnEnd", true, CultureInfo.InvariantCulture) == 0) || (string.Compare(text1, "Application_End", true, CultureInfo.InvariantCulture) == 0))
      {
            this._onEndMethod = m;
            this._onEndParamCount = infoArray1.Length;
      }
      else if ((string.Compare(text1, "Session_OnEnd", true, CultureInfo.InvariantCulture) == 0) || (string.Compare(text1, "Session_End", true, CultureInfo.InvariantCulture) == 0))
      {
            this._sessionOnEndMethod = m;
            this._sessionOnEndParamCount = infoArray1.Length;
      }
      return true;
}

 

Published by

Alfred Myers

I have been interested in computers since I got my hands on a magazine about digital electronics back in 1983 and programming them has been paying the bills since 1991. Having focused on Microsoft-centric technology stacks for the best part of two decades, in recent years I’ve been educating myself on open source technologies such as Linux, networking and the open web platform.