Merge pull request #3199 from lambdageek/dev/proxy-setter
[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.CompilerServices;
32 using System.Runtime.InteropServices;
33
34 namespace System.Reflection {
35
36         [ComVisible (true)]
37         [ComDefaultInterfaceAttribute (typeof (_EventInfo))]
38         [Serializable]
39         [ClassInterface(ClassInterfaceType.None)]
40         [StructLayout (LayoutKind.Sequential)]
41 #if MOBILE
42         public abstract class EventInfo : MemberInfo {
43 #else
44         public abstract class EventInfo : MemberInfo, _EventInfo {
45 #endif
46                 AddEventAdapter cached_add_event;
47
48                 public abstract EventAttributes Attributes {get;}
49
50                 public
51                 virtual
52                 Type EventHandlerType {
53                         get {
54                                 ParameterInfo[] p;
55                                 MethodInfo add = GetAddMethod (true);
56                                 p = add.GetParametersInternal ();
57                                 if (p.Length > 0) {
58                                         Type t = p [0].ParameterType;
59                                         /* is it alwasys the first arg?
60                                         if (!t.IsSubclassOf (typeof (System.Delegate)))
61                                                 throw new Exception ("no delegate in event");*/
62                                         return t;
63                                 }
64
65                                 return null;
66                         }
67                 }
68
69                 public
70                 virtual
71                 bool IsMulticast {get {return true;}}
72                 public bool IsSpecialName {get {return (Attributes & EventAttributes.SpecialName ) != 0;}}
73                 public override MemberTypes MemberType {
74                         get {return MemberTypes.Event;}
75                 }
76
77                 protected EventInfo() {
78                 }
79
80
81                 [DebuggerHidden]
82                 [DebuggerStepThrough]
83                 public
84                 virtual
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 EmptyArray<MethodInfo>.Value;
131                 }
132
133                 public MethodInfo[] GetOtherMethods () {
134                         return GetOtherMethods (false);
135                 }
136
137                 [DebuggerHidden]
138                 [DebuggerStepThrough]
139                 public
140                 virtual
141                 void RemoveEventHandler (object target, Delegate handler)
142                 {
143                         MethodInfo remove = GetRemoveMethod ();
144                         if (remove == null)
145                                 throw new InvalidOperationException ("Cannot remove a handler to an event that doesn't have a visible remove method");
146
147                         remove.Invoke (target, new object [] {handler});
148                 }
149
150                 public override bool Equals (object obj)
151                 {
152                         return obj == (object) this;
153                 }
154
155                 public override int GetHashCode ()
156                 {
157                         return base.GetHashCode ();
158                 }
159
160                 public static bool operator == (EventInfo left, EventInfo right)
161                 {
162                         if ((object)left == (object)right)
163                                 return true;
164                         if ((object)left == null ^ (object)right == null)
165                                 return false;
166                         return left.Equals (right);
167                 }
168
169                 public static bool operator != (EventInfo left, EventInfo right)
170                 {
171                         if ((object)left == (object)right)
172                                 return false;
173                         if ((object)left == null ^ (object)right == null)
174                                 return true;
175                         return !left.Equals (right);
176                 }
177
178 #if !MOBILE
179                 void _EventInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
180                 {
181                         throw new NotImplementedException ();
182                 }
183
184                 Type _EventInfo.GetType ()
185                 {
186                         // Required or object::GetType becomes virtual final
187                         return base.GetType ();
188                 }
189
190                 void _EventInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
191                 {
192                         throw new NotImplementedException ();
193                 }
194
195                 void _EventInfo.GetTypeInfoCount (out uint pcTInfo)
196                 {
197                         throw new NotImplementedException ();
198                 }
199
200                 void _EventInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
201                 {
202                         throw new NotImplementedException ();
203                 }
204 #endif
205
206                 delegate void AddEventAdapter (object _this, Delegate dele);
207
208 // this optimization cause problems with full AOT
209 // see bug https://bugzilla.xamarin.com/show_bug.cgi?id=3682
210 // do not revove the above delegate or it's field since it's required by the runtime!
211 #if !FULL_AOT_RUNTIME
212                 delegate void AddEvent<T, D> (T _this, D dele);
213                 delegate void StaticAddEvent<D> (D dele);
214
215 #pragma warning disable 169
216                 // Used via reflection
217                 static void AddEventFrame<T,D> (AddEvent<T,D> addEvent, object obj, object dele)
218                 {
219                         if (obj == null)
220                                 throw new TargetException ("Cannot add a handler to a non static event with a null target");
221                         if (!(obj is T))
222                                 throw new TargetException ("Object doesn't match target");
223                         addEvent ((T)obj, (D)dele);
224                 }
225
226                 static void StaticAddEventAdapterFrame<D> (StaticAddEvent<D> addEvent, object obj, object dele)
227                 {
228                         addEvent ((D)dele);
229                 }
230 #pragma warning restore 169
231
232                 /*
233                  * The idea behing this optimization is to use a pair of delegates to simulate the same effect of doing a reflection call.
234                  * The first delegate performs casting and boxing, the second dispatch to the add_... method.
235                  */
236                 static AddEventAdapter CreateAddEventDelegate (MethodInfo method)
237                 {
238                         Type[] typeVector;
239                         Type addHandlerType;
240                         object addHandlerDelegate;
241                         MethodInfo adapterFrame;
242                         Type addHandlerDelegateType;
243                         string frameName;
244
245                         if (method.IsStatic) {
246                                 typeVector = new Type[] { method.GetParametersInternal () [0].ParameterType };
247                                 addHandlerDelegateType = typeof (StaticAddEvent<>);
248                                 frameName = "StaticAddEventAdapterFrame";
249                         } else {
250                                 typeVector = new Type[] { method.DeclaringType, method.GetParametersInternal () [0].ParameterType };
251                                 addHandlerDelegateType = typeof (AddEvent<,>);
252                                 frameName = "AddEventFrame";
253                         }
254
255                         addHandlerType = addHandlerDelegateType.MakeGenericType (typeVector);
256 #if NET_2_1
257                         // with Silverlight a coreclr failure (e.g. Transparent caller creating a delegate on a Critical method)
258                         // would normally throw an ArgumentException, so we set throwOnBindFailure to false and check for a null
259                         // delegate that we can transform into a MethodAccessException
260                         addHandlerDelegate = Delegate.CreateDelegate (addHandlerType, method, false);
261                         if (addHandlerDelegate == null)
262                                 throw new MethodAccessException ();
263 #else
264                         addHandlerDelegate = Delegate.CreateDelegate (addHandlerType, method);
265 #endif
266                         adapterFrame = typeof (EventInfo).GetMethod (frameName, BindingFlags.Static | BindingFlags.NonPublic);
267                         adapterFrame = adapterFrame.MakeGenericMethod (typeVector);
268                         return (AddEventAdapter)Delegate.CreateDelegate (typeof (AddEventAdapter), addHandlerDelegate, adapterFrame, true);
269                 }
270 #endif
271
272                 public virtual MethodInfo AddMethod {
273                         get { return GetAddMethod (true); }
274                 }
275                 public virtual MethodInfo RaiseMethod {
276                         get { return GetRaiseMethod (true); }
277                 }
278                 public virtual MethodInfo RemoveMethod {
279                         get { return GetRemoveMethod (true); }
280                 }
281
282                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
283                 private static extern EventInfo internal_from_handle_type (IntPtr event_handle, IntPtr type_handle);
284
285                 internal static EventInfo GetEventFromHandle (Mono.RuntimeEventHandle handle)
286                 {
287                         if (handle.Value == IntPtr.Zero)
288                                 throw new ArgumentException ("The handle is invalid.");
289                         return internal_from_handle_type (handle.Value, IntPtr.Zero);
290                 }
291
292                 internal static EventInfo GetEventFromHandle (Mono.RuntimeEventHandle handle, RuntimeTypeHandle reflectedType)
293                 {
294                         if (handle.Value == IntPtr.Zero)
295                                 throw new ArgumentException ("The handle is invalid.");
296                         EventInfo ei = internal_from_handle_type (handle.Value, reflectedType.Value);
297                         if (ei == null)
298                                 throw new ArgumentException ("The event handle and the type handle are incompatible.");
299                         return ei;
300                 }
301         }
302 }