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