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