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