Merge pull request #498 from Unroll-Me/master
[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                 void _EventInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
188                 {
189                         throw new NotImplementedException ();
190                 }
191
192                 void _EventInfo.GetTypeInfoCount (out uint pcTInfo)
193                 {
194                         throw new NotImplementedException ();
195                 }
196
197                 void _EventInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
198                 {
199                         throw new NotImplementedException ();
200                 }
201
202                 delegate void AddEventAdapter (object _this, Delegate dele);
203
204 // this optimization cause problems with full AOT
205 // see bug https://bugzilla.xamarin.com/show_bug.cgi?id=3682
206 // do not revove the above delegate or it's field since it's required by the runtime!
207 #if !FULL_AOT_RUNTIME
208                 delegate void AddEvent<T, D> (T _this, D dele);
209                 delegate void StaticAddEvent<D> (D dele);
210
211 #pragma warning disable 169
212                 // Used via reflection
213                 static void AddEventFrame<T,D> (AddEvent<T,D> addEvent, object obj, object dele)
214                 {
215                         if (obj == null)
216                                 throw new TargetException ("Cannot add a handler to a non static event with a null target");
217                         if (!(obj is T))
218                                 throw new TargetException ("Object doesn't match target");
219                         addEvent ((T)obj, (D)dele);
220                 }
221
222                 static void StaticAddEventAdapterFrame<D> (StaticAddEvent<D> addEvent, object obj, object dele)
223                 {
224                         addEvent ((D)dele);
225                 }
226 #pragma warning restore 169
227
228                 /*
229                  * The idea behing this optimization is to use a pair of delegates to simulate the same effect of doing a reflection call.
230                  * The first delegate performs casting and boxing, the second dispatch to the add_... method.
231                  */
232                 static AddEventAdapter CreateAddEventDelegate (MethodInfo method)
233                 {
234                         Type[] typeVector;
235                         Type addHandlerType;
236                         object addHandlerDelegate;
237                         MethodInfo adapterFrame;
238                         Type addHandlerDelegateType;
239                         string frameName;
240
241                         if (method.IsStatic) {
242                                 typeVector = new Type[] { method.GetParameters () [0].ParameterType };
243                                 addHandlerDelegateType = typeof (StaticAddEvent<>);
244                                 frameName = "StaticAddEventAdapterFrame";
245                         } else {
246                                 typeVector = new Type[] { method.DeclaringType, method.GetParameters () [0].ParameterType };
247                                 addHandlerDelegateType = typeof (AddEvent<,>);
248                                 frameName = "AddEventFrame";
249                         }
250
251                         addHandlerType = addHandlerDelegateType.MakeGenericType (typeVector);
252 #if NET_2_1
253                         // with Silverlight a coreclr failure (e.g. Transparent caller creating a delegate on a Critical method)
254                         // would normally throw an ArgumentException, so we set throwOnBindFailure to false and check for a null
255                         // delegate that we can transform into a MethodAccessException
256                         addHandlerDelegate = Delegate.CreateDelegate (addHandlerType, method, false);
257                         if (addHandlerDelegate == null)
258                                 throw new MethodAccessException ();
259 #else
260                         addHandlerDelegate = Delegate.CreateDelegate (addHandlerType, method);
261 #endif
262                         adapterFrame = typeof (EventInfo).GetMethod (frameName, BindingFlags.Static | BindingFlags.NonPublic);
263                         adapterFrame = adapterFrame.MakeGenericMethod (typeVector);
264                         return (AddEventAdapter)Delegate.CreateDelegate (typeof (AddEventAdapter), addHandlerDelegate, adapterFrame, true);
265                 }
266 #endif
267         }
268 }