private void HookupEventHandlersForApplicationAndModules(MethodInfo[] handlers) {
            _currentModuleCollectionKey = HttpApplicationFactory.applicationFileName;

            if(null == _pipelineEventMasks) {
                Dictionary<string, RequestNotification> dict = new Dictionary<string, RequestNotification>();
                BuildEventMaskDictionary(dict);
                if(null == _pipelineEventMasks) {
                    _pipelineEventMasks = dict;
                }
            }


            for (int i = 0; i < handlers.Length; i++) {
                MethodInfo appMethod = handlers[i];
                String appMethodName = appMethod.Name;
                int namePosIndex = appMethodName.IndexOf('_');
                String targetName = appMethodName.Substring(0, namePosIndex);

                // Find target for method
                Object target = null;

                if (StringUtil.EqualsIgnoreCase(targetName, "Application"))
                    target = this;
                else if (_moduleCollection != null)
                    target = _moduleCollection[targetName];

                if (target == null)
                    continue;

                // Find event on the module type
                Type targetType = target.GetType();
                EventDescriptorCollection events = TypeDescriptor.GetEvents(targetType);
                string eventName = appMethodName.Substring(namePosIndex+1);

                EventDescriptor foundEvent = events.Find(eventName, true);
                if (foundEvent == null
                    && StringUtil.EqualsIgnoreCase(eventName.Substring(0, 2), "on")) {

                    eventName = eventName.Substring(2);
                    foundEvent = events.Find(eventName, true);
                }

                MethodInfo addMethod = null;
                if (foundEvent != null) {
                    EventInfo reflectionEvent = targetType.GetEvent(foundEvent.Name);
                    Debug.Assert(reflectionEvent != null);
                    if (reflectionEvent != null) {
                        addMethod = reflectionEvent.GetAddMethod();
                    }
                }

                if (addMethod == null)
                    continue;

                ParameterInfo[] addMethodParams = addMethod.GetParameters();

                if (addMethodParams.Length != 1)
                    continue;

                // Create the delegate from app method to pass to AddXXX(handler) method

                Delegate handlerDelegate = null;

                ParameterInfo[] appMethodParams = appMethod.GetParameters();

                if (appMethodParams.Length == 0) {
                    // If the app method doesn't have arguments --
                    // -- hookup via intermidiate handler

                    // only can do it for EventHandler, not strongly typed
                    if (addMethodParams[0].ParameterType != typeof(System.EventHandler))
                        continue;

                    ArglessEventHandlerProxy proxy = new ArglessEventHandlerProxy(this, appMethod);
                    handlerDelegate = proxy.Handler;
                }
                else {
                    // Hookup directly to the app methods hoping all types match

                    try {
                        handlerDelegate = Delegate.CreateDelegate(addMethodParams[0].ParameterType, this, appMethodName);
                    }
                    catch {
                        // some type mismatch
                        continue;
                    }
                }

                // Call the AddXXX() to hook up the delegate

                try {
                    addMethod.Invoke(target, new Object[1]{handlerDelegate});
                }
                catch {
                    if (HttpRuntime.UseIntegratedPipeline) {
                        throw;
                    }
                }

                if (eventName != null) {
                    if (_pipelineEventMasks.ContainsKey(eventName)) {
                        if (!StringUtil.StringStartsWith(eventName, "Post")) {
                            _appRequestNotifications |= _pipelineEventMasks[eventName];
                        }
                        else {
                            _appPostNotifications |= _pipelineEventMasks[eventName];
                        }
                    }
                }
            }
        }
 private void HookupEventHandlersForApplicationAndModules(MethodInfo[] handlers)
 {
     this._currentModuleCollectionKey = "global.asax";
     if (this._pipelineEventMasks == null)
     {
         Dictionary<string, RequestNotification> eventMask = new Dictionary<string, RequestNotification>();
         this.BuildEventMaskDictionary(eventMask);
         if (this._pipelineEventMasks == null)
         {
             this._pipelineEventMasks = eventMask;
         }
     }
     for (int i = 0; i < handlers.Length; i++)
     {
         MethodInfo arglessMethod = handlers[i];
         string name = arglessMethod.Name;
         int index = name.IndexOf('_');
         string str2 = name.Substring(0, index);
         object obj2 = null;
         if (StringUtil.EqualsIgnoreCase(str2, "Application"))
         {
             obj2 = this;
         }
         else if (this._moduleCollection != null)
         {
             obj2 = this._moduleCollection[str2];
         }
         if (obj2 != null)
         {
             Type componentType = obj2.GetType();
             EventDescriptorCollection events = TypeDescriptor.GetEvents(componentType);
             string str3 = name.Substring(index + 1);
             EventDescriptor descriptor = events.Find(str3, true);
             if ((descriptor == null) && StringUtil.EqualsIgnoreCase(str3.Substring(0, 2), "on"))
             {
                 str3 = str3.Substring(2);
                 descriptor = events.Find(str3, true);
             }
             MethodInfo addMethod = null;
             if (descriptor != null)
             {
                 EventInfo info3 = componentType.GetEvent(descriptor.Name);
                 if (info3 != null)
                 {
                     addMethod = info3.GetAddMethod();
                 }
             }
             if (addMethod != null)
             {
                 ParameterInfo[] parameters = addMethod.GetParameters();
                 if (parameters.Length == 1)
                 {
                     Delegate handler = null;
                     if (arglessMethod.GetParameters().Length == 0)
                     {
                         if (parameters[0].ParameterType != typeof(EventHandler))
                         {
                             continue;
                         }
                         ArglessEventHandlerProxy proxy = new ArglessEventHandlerProxy(this, arglessMethod);
                         handler = proxy.Handler;
                     }
                     else
                     {
                         try
                         {
                             handler = Delegate.CreateDelegate(parameters[0].ParameterType, this, name);
                         }
                         catch
                         {
                             continue;
                         }
                     }
                     try
                     {
                         addMethod.Invoke(obj2, new object[] { handler });
                     }
                     catch
                     {
                         if (HttpRuntime.UseIntegratedPipeline)
                         {
                             throw;
                         }
                     }
                     if ((str3 != null) && this._pipelineEventMasks.ContainsKey(str3))
                     {
                         if (!StringUtil.StringStartsWith(str3, "Post"))
                         {
                             this._appRequestNotifications |= (RequestNotification) this._pipelineEventMasks[str3];
                         }
                         else
                         {
                             this._appPostNotifications |= (RequestNotification) this._pipelineEventMasks[str3];
                         }
                     }
                 }
             }
         }
     }
 }