17f6b85dddf5bbc3a07a2fb083e6d1b01e7e87a9
[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         [ComVisible (true)]
36         [ComDefaultInterfaceAttribute (typeof (_EventInfo))]
37         [Serializable]
38         [ClassInterface(ClassInterfaceType.None)]
39         [StructLayout (LayoutKind.Sequential)]
40         public abstract class EventInfo : MemberInfo, _EventInfo {
41                 AddEventAdapter cached_add_event;
42
43                 public abstract EventAttributes Attributes {get;}
44
45                 public
46 #if NET_4_0 || MOONLIGHT
47                 virtual
48 #endif
49                 Type EventHandlerType {
50                         get {
51                                 ParameterInfo[] p;
52                                 MethodInfo add = GetAddMethod (true);
53                                 p = add.GetParameters ();
54                                 if (p.Length > 0) {
55                                         Type t = p [0].ParameterType;
56                                         /* is it alwasys the first arg?
57                                         if (!t.IsSubclassOf (typeof (System.Delegate)))
58                                                 throw new Exception ("no delegate in event");*/
59                                         return t;
60                                 } else
61                                         return null;
62                         }
63                 }
64
65                 public
66 #if NET_4_0 || MOONLIGHT
67                 virtual
68 #endif
69                 bool IsMulticast {get {return true;}}
70                 public bool IsSpecialName {get {return (Attributes & EventAttributes.SpecialName ) != 0;}}
71                 public override MemberTypes MemberType {
72                         get {return MemberTypes.Event;}
73                 }
74
75                 protected EventInfo() {
76                 }
77
78
79                 [DebuggerHidden]
80                 [DebuggerStepThrough]
81                 public
82 #if NET_4_0 || MOONLIGHT
83                 virtual
84 #endif
85                 void AddEventHandler (object target, Delegate handler)
86                 {
87 // this optimization cause problems with full AOT
88 // see bug https://bugzilla.xamarin.com/show_bug.cgi?id=3682
89 #if FULL_AOT_RUNTIME
90                         MethodInfo add = GetAddMethod ();
91                         if (add == null)
92                                 throw new InvalidOperationException ("Cannot add a handler to an event that doesn't have a visible add method");
93                         if (target == null && !add.IsStatic)
94                                 throw new TargetException ("Cannot add a handler to a non static event with a null target");
95                         add.Invoke (target, new object [] {handler});
96 #else
97                         if (cached_add_event == null) {
98                                 MethodInfo add = GetAddMethod ();
99                                 if (add == null)
100                                         throw new InvalidOperationException ("Cannot add a handler to an event that doesn't have a visible add method");
101                                 if (add.DeclaringType.IsValueType) {
102                                         if (target == null && !add.IsStatic)
103                                                 throw new TargetException ("Cannot add a handler to a non static event with a null target");
104                                         add.Invoke (target, new object [] {handler});
105                                         return;
106                                 }
107                                 cached_add_event = CreateAddEventDelegate (add);
108                         }
109                         //if (target == null && is_instance)
110                         //      throw new TargetException ("Cannot add a handler to a non static event with a null target");
111                         cached_add_event (target, handler);
112 #endif
113                 }
114
115                 public MethodInfo GetAddMethod() {
116                         return GetAddMethod (false);
117                 }
118                 public abstract MethodInfo GetAddMethod(bool nonPublic);
119                 public MethodInfo GetRaiseMethod() {
120                         return GetRaiseMethod (false);
121                 }
122                 public abstract MethodInfo GetRaiseMethod( bool nonPublic);
123                 public MethodInfo GetRemoveMethod() {
124                         return GetRemoveMethod (false);
125                 }
126                 public abstract MethodInfo GetRemoveMethod( bool nonPublic);
127
128                 public virtual MethodInfo[] GetOtherMethods (bool nonPublic) {
129                         // implemented by the derived class
130                         return new MethodInfo [0];
131                 }
132
133                 public MethodInfo[] GetOtherMethods () {
134                         return GetOtherMethods (false);
135                 }
136
137                 [DebuggerHidden]
138                 [DebuggerStepThrough]
139                 public
140 #if NET_4_0 || MOONLIGHT
141                 virtual
142 #endif
143                 void RemoveEventHandler (object target, Delegate handler)
144                 {
145                         MethodInfo remove = GetRemoveMethod ();
146                         if (remove == null)
147                                 throw new InvalidOperationException ("Cannot remove a handler to an event that doesn't have a visible remove method");
148
149                         remove.Invoke (target, new object [] {handler});
150                 }
151
152 #if NET_4_0
153                 public override bool Equals (object obj)
154                 {
155                         return obj == (object) this;
156                 }
157
158                 public override int GetHashCode ()
159                 {
160                         return base.GetHashCode ();
161                 }
162
163                 public static bool operator == (EventInfo left, EventInfo right)
164                 {
165                         if ((object)left == (object)right)
166                                 return true;
167                         if ((object)left == null ^ (object)right == null)
168                                 return false;
169                         return left.Equals (right);
170                 }
171
172                 public static bool operator != (EventInfo left, EventInfo right)
173                 {
174                         if ((object)left == (object)right)
175                                 return false;
176                         if ((object)left == null ^ (object)right == null)
177                                 return true;
178                         return !left.Equals (right);
179                 }
180 #endif
181
182                 void _EventInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
183                 {
184                         throw new NotImplementedException ();
185                 }
186
187                 Type _EventInfo.GetType ()
188                 {
189                         // Required or object::GetType becomes virtual final
190                         return base.GetType ();
191                 }
192
193                 void _EventInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
194                 {
195                         throw new NotImplementedException ();
196                 }
197
198                 void _EventInfo.GetTypeInfoCount (out uint pcTInfo)
199                 {
200                         throw new NotImplementedException ();
201                 }
202
203                 void _EventInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
204                 {
205                         throw new NotImplementedException ();
206                 }
207
208                 delegate void AddEventAdapter (object _this, Delegate dele);
209
210 // this optimization cause problems with full AOT
211 // see bug https://bugzilla.xamarin.com/show_bug.cgi?id=3682
212 // do not revove the above delegate or it's field since it's required by the runtime!
213 #if !FULL_AOT_RUNTIME
214                 delegate void AddEvent<T, D> (T _this, D dele);
215                 delegate void StaticAddEvent<D> (D dele);
216
217 #pragma warning disable 169
218                 // Used via reflection
219                 static void AddEventFrame<T,D> (AddEvent<T,D> addEvent, object obj, object dele)
220                 {
221                         if (obj == null)
222                                 throw new TargetException ("Cannot add a handler to a non static event with a null target");
223                         if (!(obj is T))
224                                 throw new TargetException ("Object doesn't match target");
225                         addEvent ((T)obj, (D)dele);
226                 }
227
228                 static void StaticAddEventAdapterFrame<D> (StaticAddEvent<D> addEvent, object obj, object dele)
229                 {
230                         addEvent ((D)dele);
231                 }
232 #pragma warning restore 169
233
234                 /*
235                  * The idea behing this optimization is to use a pair of delegates to simulate the same effect of doing a reflection call.
236                  * The first delegate performs casting and boxing, the second dispatch to the add_... method.
237                  */
238                 static AddEventAdapter CreateAddEventDelegate (MethodInfo method)
239                 {
240                         Type[] typeVector;
241                         Type addHandlerType;
242                         object addHandlerDelegate;
243                         MethodInfo adapterFrame;
244                         Type addHandlerDelegateType;
245                         string frameName;
246
247                         if (method.IsStatic) {
248                                 typeVector = new Type[] { method.GetParameters () [0].ParameterType };
249                                 addHandlerDelegateType = typeof (StaticAddEvent<>);
250                                 frameName = "StaticAddEventAdapterFrame";
251                         } else {
252                                 typeVector = new Type[] { method.DeclaringType, method.GetParameters () [0].ParameterType };
253                                 addHandlerDelegateType = typeof (AddEvent<,>);
254                                 frameName = "AddEventFrame";
255                         }
256
257                         addHandlerType = addHandlerDelegateType.MakeGenericType (typeVector);
258 #if NET_2_1
259                         // with Silverlight a coreclr failure (e.g. Transparent caller creating a delegate on a Critical method)
260                         // would normally throw an ArgumentException, so we set throwOnBindFailure to false and check for a null
261                         // delegate that we can transform into a MethodAccessException
262                         addHandlerDelegate = Delegate.CreateDelegate (addHandlerType, method, false);
263                         if (addHandlerDelegate == null)
264                                 throw new MethodAccessException ();
265 #else
266                         addHandlerDelegate = Delegate.CreateDelegate (addHandlerType, method);
267 #endif
268                         adapterFrame = typeof (EventInfo).GetMethod (frameName, BindingFlags.Static | BindingFlags.NonPublic);
269                         adapterFrame = adapterFrame.MakeGenericMethod (typeVector);
270                         return (AddEventAdapter)Delegate.CreateDelegate (typeof (AddEventAdapter), addHandlerDelegate, adapterFrame, true);
271                 }
272 #endif
273         }
274 }