2 // System.ComponentModel.EventDescriptor.cs
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // (C) Novell, Inc. 2004, 2005
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Runtime.InteropServices;
32 using System.Collections;
33 using System.Reflection;
35 namespace System.ComponentModel
37 internal class ReflectionEventDescriptor: EventDescriptor
43 MethodInfo add_method;
44 MethodInfo remove_method;
46 public ReflectionEventDescriptor (EventInfo eventInfo) : base (eventInfo.Name, (Attribute[]) eventInfo.GetCustomAttributes (true))
48 _eventInfo = eventInfo;
49 _componentType = eventInfo.DeclaringType;
50 _eventType = eventInfo.EventHandlerType;
52 add_method = eventInfo.GetAddMethod ();
53 remove_method = eventInfo.GetRemoveMethod ();
56 public ReflectionEventDescriptor (Type componentType, EventDescriptor oldEventDescriptor, Attribute[] attrs) : base (oldEventDescriptor, attrs)
58 _componentType = componentType;
59 _eventType = oldEventDescriptor.EventType;
61 EventInfo event_info = componentType.GetEvent (oldEventDescriptor.Name);
62 add_method = event_info.GetAddMethod ();
63 remove_method = event_info.GetRemoveMethod ();
66 public ReflectionEventDescriptor (Type componentType, string name, Type type, Attribute[] attrs) : base (name, attrs)
68 _componentType = componentType;
71 EventInfo event_info = componentType.GetEvent (name);
72 add_method = event_info.GetAddMethod ();
73 remove_method = event_info.GetRemoveMethod ();
76 EventInfo GetEventInfo ()
78 if (_eventInfo == null) {
79 _eventInfo = _componentType.GetEvent (Name);
80 if (_eventInfo == null)
81 throw new ArgumentException ("Accessor methods for the " + Name + " event are missing");
86 public override void AddEventHandler (object component, System.Delegate value)
88 add_method.Invoke (component, new object [] { value });
91 public override void RemoveEventHandler (object component, System.Delegate value)
93 remove_method.Invoke (component, new object [] { value });
96 public override System.Type ComponentType
98 get { return _componentType; }
101 public override System.Type EventType
103 get { return _eventType; }
106 public override bool IsMulticast
108 get { return GetEventInfo().IsMulticast; }