New test.
[mono.git] / mcs / class / corlib / System.Reflection / EventInfo.cs
1 //
2 // System.Reflection/EventInfo.cs
3 //
4 // Author:
5 //   Paolo Molaro (lupus@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.  http://www.ximian.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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.Diagnostics;
31 using System.Runtime.InteropServices;
32
33 namespace System.Reflection {
34
35 #if NET_2_0
36         [ComVisible (true)]
37         [ComDefaultInterfaceAttribute (typeof (_EventInfo))]
38         [Serializable]
39 #endif
40         [ClassInterface(ClassInterfaceType.None)]
41         public abstract class EventInfo : MemberInfo, _EventInfo {
42 #if NET_2_0
43                 AddEventAdapter cached_add_event;
44 #else
45                 object placeholder;
46 #endif
47
48                 public abstract EventAttributes Attributes {get;}
49
50                 public Type EventHandlerType {
51                         get {
52                                 ParameterInfo[] p;
53                                 MethodInfo add = GetAddMethod (true);
54                                 p = add.GetParameters ();
55                                 if (p.Length > 0) {
56                                         Type t = p [0].ParameterType;
57                                         /* is it alwasys the first arg?
58                                         if (!t.IsSubclassOf (typeof (System.Delegate)))
59                                                 throw new Exception ("no delegate in event");*/
60                                         return t;
61                                 } else
62                                         return null;
63                         }
64                 }
65                 public bool IsMulticast {get {return true;}}
66                 public bool IsSpecialName {get {return (Attributes & EventAttributes.SpecialName ) != 0;}}
67                 public override MemberTypes MemberType {
68                         get {return MemberTypes.Event;}
69                 }
70
71                 protected EventInfo() {
72                 }
73
74 #if ONLY_1_1
75                 public new Type GetType ()
76                 {
77                         return base.GetType ();
78                 }
79 #endif
80
81                 [DebuggerHidden]
82                 [DebuggerStepThrough]
83                 public void AddEventHandler (object target, Delegate handler)
84                 {
85 #if NET_2_0
86                         if (cached_add_event == null) {
87                                 MethodInfo add = GetAddMethod ();
88                                 if (add == null)
89                                         throw new Exception ("No add method!?");
90                                 if (add.DeclaringType.IsValueType) {
91                                         add.Invoke (target, new object [] {handler});
92                                         return;
93                                 }
94                                 cached_add_event = CreateAddEventDelegate (add);
95                         }
96                         cached_add_event (target, handler);
97 #else
98                         MethodInfo add = GetAddMethod ();
99                         if (add == null)
100                                 throw new Exception ("No add method!?");
101                         add.Invoke (target, new object [] {handler});
102 #endif
103                 }
104
105                 public MethodInfo GetAddMethod() {
106                         return GetAddMethod (false);
107                 }
108                 public abstract MethodInfo GetAddMethod(bool nonPublic);
109                 public MethodInfo GetRaiseMethod() {
110                         return GetRaiseMethod (false);
111                 }
112                 public abstract MethodInfo GetRaiseMethod( bool nonPublic);
113                 public MethodInfo GetRemoveMethod() {
114                         return GetRemoveMethod (false);
115                 }
116                 public abstract MethodInfo GetRemoveMethod( bool nonPublic);
117
118 #if NET_2_0
119                 public virtual MethodInfo[] GetOtherMethods (bool nonPublic) {
120                         // implemented by the derived class
121                         return new MethodInfo [0];
122                 }
123
124                 public MethodInfo[] GetOtherMethods () {
125                         return GetOtherMethods (false);
126                 }
127 #endif          
128
129                 [DebuggerHidden]
130                 [DebuggerStepThrough]
131                 public void RemoveEventHandler (object target, Delegate handler)
132                 {
133                         MethodInfo remove = GetRemoveMethod ();
134                         if (remove == null)
135                                 throw new Exception ("No remove method!?");
136
137                         remove.Invoke (target, new object [] {handler});
138                 }
139
140                 void _EventInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
141                 {
142                         throw new NotImplementedException ();
143                 }
144
145                 void _EventInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
146                 {
147                         throw new NotImplementedException ();
148                 }
149
150                 void _EventInfo.GetTypeInfoCount (out uint pcTInfo)
151                 {
152                         throw new NotImplementedException ();
153                 }
154
155                 void _EventInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
156                 {
157                         throw new NotImplementedException ();
158                 }
159 #if NET_2_0
160                 delegate void AddEventAdapter (object _this, Delegate dele);
161                 delegate void AddEvent<T, D> (T _this, D dele);
162                 delegate void StaticAddEvent<D> (D dele);
163
164                 static void AddEventFrame<T,D> (AddEvent<T,D> addEvent, object obj, object dele)
165                 {
166                         addEvent ((T)obj, (D)dele);
167                 }
168
169                 static void StaticAddEventAdapterFrame<D> (StaticAddEvent<D> addEvent, object obj, object dele)
170                 {
171                         addEvent ((D)dele);
172                 }
173
174                 /*
175                  * The idea behing this optimization is to use a pair of delegates to simulate the same effect of doing a reflection call.
176                  * The first delegate performs casting and boxing, the second dispatch to the add_... method.
177                  */
178                 static AddEventAdapter CreateAddEventDelegate (MethodInfo method)
179                 {
180                         Type[] typeVector;
181                         Type addHandlerType;
182                         object addHandlerDelegate;
183                         MethodInfo adapterFrame;
184                         Type addHandlerDelegateType;
185                         string frameName;
186
187                         if (method.IsStatic) {
188                                 typeVector = new Type[] { method.GetParameters () [0].ParameterType };
189                                 addHandlerDelegateType = typeof (StaticAddEvent<>);
190                                 frameName = "StaticAddEventAdapterFrame";
191                         } else {
192                                 typeVector = new Type[] { method.DeclaringType, method.GetParameters () [0].ParameterType };
193                                 addHandlerDelegateType = typeof (AddEvent<,>);
194                                 frameName = "AddEventFrame";
195                         }
196
197                         addHandlerType = addHandlerDelegateType.MakeGenericType (typeVector);
198 #if NET_2_1
199                         // with Silverlight a coreclr failure (e.g. Transparent caller creating a delegate on a Critical method)
200                         // would normally throw an ArgumentException, so we set throwOnBindFailure to false and check for a null
201                         // delegate that we can transform into a MethodAccessException
202                         addHandlerDelegate = Delegate.CreateDelegate (addHandlerType, method, false);
203                         if (addHandlerDelegate == null)
204                                 throw new MethodAccessException ();
205 #else
206                         addHandlerDelegate = Delegate.CreateDelegate (addHandlerType, method);
207 #endif
208                         adapterFrame = typeof (EventInfo).GetMethod (frameName, BindingFlags.Static | BindingFlags.NonPublic);
209                         adapterFrame = adapterFrame.MakeGenericMethod (typeVector);
210                         return (AddEventAdapter)Delegate.CreateDelegate (typeof (AddEventAdapter), addHandlerDelegate, adapterFrame, true);
211                 }
212 #endif
213         }
214 }