New test.
[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, 2005
8 //
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using System;
31 using System.Runtime.InteropServices;
32 using System.Collections;
33 using System.Reflection;
34
35 namespace System.ComponentModel
36 {
37         internal class ReflectionEventDescriptor: EventDescriptor
38         {
39                 Type _eventType;
40                 Type _componentType;
41                 EventInfo _eventInfo;
42
43                 MethodInfo add_method;
44                 MethodInfo remove_method;
45
46                 public ReflectionEventDescriptor (EventInfo eventInfo) : base (eventInfo.Name, (Attribute[]) eventInfo.GetCustomAttributes (true))
47                 {
48                         _eventInfo = eventInfo;
49                         _componentType = eventInfo.DeclaringType;
50                         _eventType = eventInfo.EventHandlerType;
51
52                         add_method = eventInfo.GetAddMethod ();
53                         remove_method = eventInfo.GetRemoveMethod ();
54                 }
55
56                 public ReflectionEventDescriptor (Type componentType, EventDescriptor oldEventDescriptor, Attribute[] attrs) : base (oldEventDescriptor, attrs)
57                 {
58                         _componentType = componentType;
59                         _eventType = oldEventDescriptor.EventType;
60
61                         EventInfo event_info = componentType.GetEvent (oldEventDescriptor.Name);
62                         add_method = event_info.GetAddMethod ();
63                         remove_method = event_info.GetRemoveMethod ();
64                 }
65
66                 public ReflectionEventDescriptor (Type componentType, string name, Type type, Attribute[] attrs) : base (name, attrs)
67                 {
68                         _componentType = componentType;
69                         _eventType = type;
70
71                         EventInfo event_info = componentType.GetEvent (name);
72                         add_method = event_info.GetAddMethod ();
73                         remove_method = event_info.GetRemoveMethod ();
74                 }
75                 
76                 EventInfo GetEventInfo ()
77                 {
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");
82                         }
83                         return _eventInfo;
84                 }
85
86                 public override void AddEventHandler (object component, System.Delegate value)
87                 {
88                         add_method.Invoke (component, new object [] { value });
89                 }
90
91                 public override void RemoveEventHandler (object component, System.Delegate value)
92                 {
93                         remove_method.Invoke (component, new object [] { value });
94                 }
95
96                 public override System.Type ComponentType
97                 { 
98                         get { return _componentType; }
99                 }
100
101                 public override System.Type EventType
102                 { 
103                         get { return _eventType; }
104                 }
105
106                 public override bool IsMulticast 
107                 {
108                         get { return GetEventInfo().IsMulticast; }
109                 }
110         }
111 }