**** Merged from MCS ****
[mono-project.git] / mcs / class / System / System.ComponentModel / ReflectionEventDescriptor.cs
blobc2ad8410fa8e29e26f3f012c6f733559bc1d9933
1 //
2 // System.ComponentModel.EventDescriptor.cs
3 //
4 // Authors:
5 // Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // (C) Novell, Inc. 2004
8 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System;
32 using System.Runtime.InteropServices;
33 using System.Collections;
34 using System.Reflection;
36 namespace System.ComponentModel
38 internal class ReflectionEventDescriptor: EventDescriptor
40 Hashtable handlers;
41 Type _eventType;
42 Type _componentType;
43 EventInfo _eventInfo;
45 public ReflectionEventDescriptor (EventInfo eventInfo) : base (eventInfo.Name, (Attribute[]) eventInfo.GetCustomAttributes (true))
47 _eventInfo = eventInfo;
48 _componentType = eventInfo.DeclaringType;
49 _eventType = eventInfo.EventHandlerType;
52 public ReflectionEventDescriptor (Type componentType, EventDescriptor oldEventDescriptor, Attribute[] attrs) : base (oldEventDescriptor, attrs)
54 _componentType = componentType;
55 _eventType = oldEventDescriptor.EventType;
58 public ReflectionEventDescriptor (Type componentType, string name, Type type, Attribute[] attrs) : base (name, attrs)
60 _componentType = componentType;
61 _eventType = type;
64 EventInfo GetEventInfo ()
66 if (_eventInfo == null) {
67 _eventInfo = _componentType.GetEvent (Name);
68 if (_eventInfo == null)
69 throw new ArgumentException ("Accessor methods for the " + Name + " event are missing");
71 return _eventInfo;
74 public override void AddEventHandler (object component, System.Delegate value)
76 if (handlers == null)
77 handlers = new Hashtable ();
79 ArrayList delegates = (ArrayList) handlers [component];
80 if (delegates == null) {
81 delegates = new ArrayList ();
82 handlers [component] = delegates;
85 if (!delegates.Contains (value))
86 delegates.Add (value);
89 public override void RemoveEventHandler (object component, System.Delegate value)
91 if (handlers == null) return;
93 ArrayList delegates = (ArrayList) handlers [component];
94 if (delegates == null) return;
96 delegates.Remove (value);
99 public override System.Type ComponentType
101 get { return _componentType; }
104 public override System.Type EventType
106 get { return _eventType; }
109 public override bool IsMulticast
111 get { return GetEventInfo().IsMulticast; }