Inheritance: System.ComponentModel.EventDescriptor
        public ReflectEventDescriptor(Type componentType, EventDescriptor oldReflectEventDescriptor, Attribute[] attributes) : base(oldReflectEventDescriptor, attributes)
        {
            this.componentClass = componentType;
            this.type           = oldReflectEventDescriptor.EventType;
            ReflectEventDescriptor descriptor = oldReflectEventDescriptor as ReflectEventDescriptor;

            if (descriptor != null)
            {
                this.addMethod     = descriptor.addMethod;
                this.removeMethod  = descriptor.removeMethod;
                this.filledMethods = true;
            }
        }
        /// <summary>
        ///     This constructor takes an existing ReflectEventDescriptor and modifies it by merging in the
        ///     passed-in attributes.
        /// </summary>
        public ReflectEventDescriptor(Type componentType, EventDescriptor oldReflectEventDescriptor, Attribute[] attributes)
            : base(oldReflectEventDescriptor, attributes)
        {
            _componentClass = componentType;
            _type           = oldReflectEventDescriptor.EventType;

            ReflectEventDescriptor desc = oldReflectEventDescriptor as ReflectEventDescriptor;

            if (desc != null)
            {
                _addMethod     = desc._addMethod;
                _removeMethod  = desc._removeMethod;
                _filledMethods = true;
            }
        }
Ejemplo n.º 3
0
        /// <devdoc>
        ///     This constructor takes an existing ReflectEventDescriptor and modifies it by merging in the
        ///     passed-in attributes.
        /// </devdoc>
        public ReflectEventDescriptor(Type componentType, EventDescriptor oldReflectEventDescriptor, Attribute[] attributes)
            : base(oldReflectEventDescriptor, attributes)
        {
            this.componentClass = componentType;
            this.type           = oldReflectEventDescriptor.EventType;

            ReflectEventDescriptor desc = oldReflectEventDescriptor as ReflectEventDescriptor;

            if (desc != null)
            {
                this.addMethod     = desc.addMethod;
                this.removeMethod  = desc.removeMethod;
                this.filledMethods = true;
            }
            #if DEBUG
            else if (oldReflectEventDescriptor is DebugReflectEventDescriptor)
            {
                this.addMethod     = ((DebugReflectEventDescriptor)oldReflectEventDescriptor).addMethod;
                this.removeMethod  = ((DebugReflectEventDescriptor)oldReflectEventDescriptor).removeMethod;
                this.filledMethods = true;
            }
            #endif
        }
        /// <devdoc>
        ///     Static helper API around reflection to get and cache
        ///     events.  This does not recurse to the base class.
        /// </devdoc>
        private static EventDescriptor[] ReflectGetEvents(Type type)
        {
            if (_eventCache == null)
            {
                lock (_internalSyncObject)
                {
                    if (_eventCache == null)
                    {
                        _eventCache = new Hashtable();
                    }
                }
            }

            EventDescriptor[] events = (EventDescriptor[])_eventCache[type];
            if (events != null)
            {
                return events;
            }

            lock (_internalSyncObject)
            {
                events = (EventDescriptor[])_eventCache[type];
                if (events == null)
                {
                    BindingFlags bindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance;
                    TypeDescriptor.Trace("Events : Building events for {0}", type.Name);

                    // Get the type's events.  Events may have their add and
                    // remove methods individually overridden in a derived
                    // class, but at some point in the base class chain both
                    // methods must exist.  If we find an event that doesn't
                    // have both add and remove, we skip it here, because it
                    // will be picked up in our base class scan.
                    //
                    EventInfo[] eventInfos = type.GetEvents(bindingFlags);
                    events = new EventDescriptor[eventInfos.Length];
                    int eventCount = 0;

                    for (int idx = 0; idx < eventInfos.Length; idx++)
                    {
                        EventInfo eventInfo = eventInfos[idx];

                        // GetEvents returns events that are on nonpublic types
                        // if those types are from our assembly.  Screen these.
                        // 
                        if ((!(eventInfo.DeclaringType.IsPublic || eventInfo.DeclaringType.IsNestedPublic)) && (eventInfo.DeclaringType.Assembly == typeof(ReflectTypeDescriptionProvider).Assembly)) {
                            Debug.Fail("Hey, assumption holds true.  Rip this assert.");
                            continue;
                        }

                        MethodInfo addMethod = eventInfo.GetAddMethod();
                        MethodInfo removeMethod = eventInfo.GetRemoveMethod();

                        if (addMethod != null && removeMethod != null)
                        {
                            events[eventCount++] = new ReflectEventDescriptor(type, eventInfo);
                        }
                    }

                    if (eventCount != events.Length)
                    {
                        EventDescriptor[] newEvents = new EventDescriptor[eventCount];
                        Array.Copy(events, 0, newEvents, 0, eventCount);
                        events = newEvents;
                    }

                    #if DEBUG
                    foreach(EventDescriptor dbgEvent in events)
                    {
                        Debug.Assert(dbgEvent != null, "Holes in event array for type " + type);
                    }
                    #endif
                    _eventCache[type] = events;
                }
            }

            return events;
        }
 private static EventDescriptor[] ReflectGetEvents(Type type)
 {
     if (_eventCache == null)
     {
         lock (_internalSyncObject)
         {
             if (_eventCache == null)
             {
                 _eventCache = new Hashtable();
             }
         }
     }
     EventDescriptor[] sourceArray = (EventDescriptor[]) _eventCache[type];
     if (sourceArray == null)
     {
         lock (_internalSyncObject)
         {
             sourceArray = (EventDescriptor[]) _eventCache[type];
             if (sourceArray != null)
             {
                 return sourceArray;
             }
             BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly;
             EventInfo[] events = type.GetEvents(bindingAttr);
             sourceArray = new EventDescriptor[events.Length];
             int length = 0;
             for (int i = 0; i < events.Length; i++)
             {
                 EventInfo eventInfo = events[i];
                 if ((eventInfo.DeclaringType.IsPublic || eventInfo.DeclaringType.IsNestedPublic) || (eventInfo.DeclaringType.Assembly != typeof(ReflectTypeDescriptionProvider).Assembly))
                 {
                     MethodInfo addMethod = eventInfo.GetAddMethod();
                     MethodInfo removeMethod = eventInfo.GetRemoveMethod();
                     if ((addMethod != null) && (removeMethod != null))
                     {
                         sourceArray[length++] = new ReflectEventDescriptor(type, eventInfo);
                     }
                 }
             }
             if (length != sourceArray.Length)
             {
                 EventDescriptor[] destinationArray = new EventDescriptor[length];
                 Array.Copy(sourceArray, 0, destinationArray, 0, length);
                 sourceArray = destinationArray;
             }
             _eventCache[type] = sourceArray;
         }
     }
     return sourceArray;
 }