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

// In HttpApplicationFactory

private void HookupEventHandlersForAppplicationAndModules(MethodInfo[] handlers)
{
      // handlers receives a list of Global’s methods.
      for (int num1 = 0; num1 < handlers.Length; num1++)
      {
            MethodInfo info1 = handlers[num1];
            string text1 = info1.Name;
            int num2 = text1.IndexOf('_'); // Splits the method name to get the object and method’s name.
            string text2 = text1.Substring(0, num2);
            object obj1 = null;
            if (string.Compare(text2, "Application", true, CultureInfo.InvariantCulture) == 0)
            {
                  obj1 = this// Maps “Application_*” to HttpApplication
            }
            else if (this._moduleCollection != null)
            {
                  obj1 = this._moduleCollection[text2];
            }
            if (obj1 != null)
            {
                  // Tries to get an EventDescription that matches a method in Global.
                  Type type1 = obj1.GetType();
                  EventDescriptorCollection collection1 = TypeDescriptor.GetEvents(type1);
                  string text3 = text1.Substring(num2 + 1);
                  EventDescriptor descriptor1 = collection1.Find(text3, true);
                  if ((descriptor1 == null) && (string.Compare(text3.Substring(0, 2), "on", true, CultureInfo.InvariantCulture) == 0))
                  {
                        text3 = text3.Substring(2);
                        descriptor1 = collection1.Find(text3, true);
                  }
                  // Gets a method that corresponds to the “+” in Type.Event += EventHandler
                  MethodInfo info2 = null;
                  if (descriptor1 != null)
                  {
                        EventInfo info3 = type1.GetEvent(descriptor1.Name);
                        if (info3 != null)
                        {
                              info2 = info3.GetAddMethod();
                        }
                  }
                  if (info2 != null)
                  {
                        ParameterInfo[] infoArray1 = info2.GetParameters();
                        if (infoArray1.Length == 1)
                        {
                              Delegate delegate1 = null;
                              ParameterInfo[] infoArray2 = info1.GetParameters();
                              if (infoArray2.Length == 0)
                              {
                                    if (infoArray1[0].ParameterType != typeof(EventHandler))
                                    {
                                          goto Label_0168;
                                    }
                                    ArglessEventHandlerProxy proxy1 = new ArglessEventHandlerProxy(this, info1);
                                    delegate1 = proxy1.Handler;
                              }
                              else
                              {
                                    try
                                    {
                                          // Creates the delegate. Corresponds to “new EventHandler(metodoDeGlobal)”
                                          delegate1 = Delegate.CreateDelegate(infoArray1[0].ParameterType, this, text1);
                                    }
                                    catch (Exception)
                                    {
                                          goto Label_0168;
                                    }
                              }
                              try
                              {
                                    object[] objArray1 = new object[1] { delegate1 } ;
                                    // Binds the method to the event. 
                                    // Corresponds to executing the “+” in Type.Event += EventHandler                                    info2.Invoke(obj1, objArray1);
                              }
                              catch (Exception)
                              {
                              }
                        }
                  }
            }
      Label_0168:;
      }

}

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.