2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System / System.ComponentModel / ReflectionEventDescriptor.cs
1 //
2 // System.ComponentModel.EventDescriptor.cs
3 //
4 // Authors:
5 //  Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // (C) Novell, Inc. 2004
8 //
9
10 //
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.
29 //
30
31 using System;
32 using System.Runtime.InteropServices;
33 using System.Collections;
34 using System.Reflection;
35
36 namespace System.ComponentModel
37 {
38         internal class ReflectionEventDescriptor: EventDescriptor
39         {
40                 Hashtable handlers;
41                 Type _eventType;
42                 Type _componentType;
43                 EventInfo _eventInfo;
44                 
45                 public ReflectionEventDescriptor (EventInfo eventInfo) : base (eventInfo.Name, (Attribute[]) eventInfo.GetCustomAttributes (true))
46                 {
47                         _eventInfo = eventInfo;
48                         _componentType = eventInfo.DeclaringType;
49                         _eventType = eventInfo.EventHandlerType;
50                 }
51
52                 public ReflectionEventDescriptor (Type componentType, EventDescriptor oldEventDescriptor, Attribute[] attrs) : base (oldEventDescriptor, attrs)
53                 {
54                         _componentType = componentType;
55                         _eventType = oldEventDescriptor.EventType;
56                 }
57
58                 public ReflectionEventDescriptor (Type componentType, string name, Type type, Attribute[] attrs) : base (name, attrs)
59                 {
60                         _componentType = componentType;
61                         _eventType = type;
62                 }
63                 
64                 EventInfo GetEventInfo ()
65                 {
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");
70                         }
71                         return _eventInfo;
72                 }
73
74                 public override void AddEventHandler (object component, System.Delegate value)
75                 {
76                         if (handlers == null)
77                                 handlers = new Hashtable ();
78                         
79                         ArrayList delegates = (ArrayList) handlers [component];
80                         if (delegates == null) {
81                                 delegates = new ArrayList ();
82                                 handlers [component] = delegates;
83                         }
84                         
85                         if (!delegates.Contains (value))
86                                 delegates.Add (value);
87                 }
88
89                 public override void RemoveEventHandler (object component, System.Delegate value)
90                 {
91                         if (handlers == null) return;
92                         
93                         ArrayList delegates = (ArrayList) handlers [component];
94                         if (delegates == null) return;
95                         
96                         delegates.Remove (value);
97                 }
98
99                 public override System.Type ComponentType
100                 { 
101                         get { return _componentType; }
102                 }
103
104                 public override System.Type EventType
105                 { 
106                         get { return _eventType; }
107                 }
108
109                 public override bool IsMulticast 
110                 {
111                         get { return GetEventInfo().IsMulticast; }
112                 }
113         }
114 }