System.ComponentModel.DebugReflectEventDescriptor.AddEventHandler C# (CSharp) Method

AddEventHandler() public method

public AddEventHandler ( object component, Delegate value ) : void
component object
value System.Delegate
return void
        public override void AddEventHandler(object component, Delegate value) {
            FillMethods();

            if (component != null) {
                ISite site = GetSite(component);
                IComponentChangeService changeService = null;

                // Announce that we are about to change this component
                //
                if (site != null) {
                    changeService = (IComponentChangeService)site.GetService(typeof(IComponentChangeService));
                    Debug.Assert(!CompModSwitches.CommonDesignerServices.Enabled || changeService != null, "IComponentChangeService not found");
                }

                if (changeService != null) {
                    try {
                        changeService.OnComponentChanging(component, this);
                    }
                    catch (CheckoutException coEx) {
                        if (coEx == CheckoutException.Canceled) {
                            return;
                        }
                        throw coEx;
                    }
                }

                bool shadowed = false;

                if (site != null && site.DesignMode) {
                    // Events are final, so just check the class
                    if (EventType != value.GetType()) {
                        throw new ArgumentException(SR.GetString(SR.ErrorInvalidEventHandler, Name));
                    }
                    IDictionaryService dict = (IDictionaryService)site.GetService(typeof(IDictionaryService));
                    Debug.Assert(!CompModSwitches.CommonDesignerServices.Enabled || dict != null, "IDictionaryService not found");
                    if (dict != null) {
                        Delegate eventdesc = (Delegate)dict.GetValue(this);
                        eventdesc = Delegate.Combine(eventdesc, value);
                        dict.SetValue(this, eventdesc);
                        shadowed = true;
                    }
                }

                if (!shadowed) {
                    addMethod.Invoke(component, new object[] { value});
                }

                // Now notify the change service that the change was successful.
                //
                if (changeService != null) {
                    changeService.OnComponentChanged(component, this, null, value);
                }
            }
        }