2 // System.Reflection/EventInfo.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Diagnostics;
31 using System.Runtime.CompilerServices;
32 using System.Runtime.InteropServices;
34 namespace System.Reflection {
37 [ComDefaultInterfaceAttribute (typeof (_EventInfo))]
39 [ClassInterface(ClassInterfaceType.None)]
40 [StructLayout (LayoutKind.Sequential)]
42 public abstract class EventInfo : MemberInfo {
44 public abstract class EventInfo : MemberInfo, _EventInfo {
46 AddEventAdapter cached_add_event;
48 public abstract EventAttributes Attributes {get;}
52 Type EventHandlerType {
55 MethodInfo add = GetAddMethod (true);
56 p = add.GetParametersInternal ();
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");*/
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;}
77 protected EventInfo() {
85 void AddEventHandler (object target, Delegate handler)
87 // this optimization cause problems with full AOT
88 // see bug https://bugzilla.xamarin.com/show_bug.cgi?id=3682
90 MethodInfo add = GetAddMethod ();
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});
97 if (cached_add_event == null) {
98 MethodInfo add = GetAddMethod ();
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});
107 cached_add_event = CreateAddEventDelegate (add);
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);
115 public MethodInfo GetAddMethod() {
116 return GetAddMethod (false);
118 public abstract MethodInfo GetAddMethod(bool nonPublic);
119 public MethodInfo GetRaiseMethod() {
120 return GetRaiseMethod (false);
122 public abstract MethodInfo GetRaiseMethod( bool nonPublic);
123 public MethodInfo GetRemoveMethod() {
124 return GetRemoveMethod (false);
126 public abstract MethodInfo GetRemoveMethod( bool nonPublic);
128 public virtual MethodInfo[] GetOtherMethods (bool nonPublic) {
129 // implemented by the derived class
130 return EmptyArray<MethodInfo>.Value;
133 public MethodInfo[] GetOtherMethods () {
134 return GetOtherMethods (false);
138 [DebuggerStepThrough]
141 void RemoveEventHandler (object target, Delegate handler)
143 MethodInfo remove = GetRemoveMethod ();
145 throw new InvalidOperationException ("Cannot remove a handler to an event that doesn't have a visible remove method");
147 remove.Invoke (target, new object [] {handler});
150 public override bool Equals (object obj)
152 return obj == (object) this;
155 public override int GetHashCode ()
157 return base.GetHashCode ();
160 public static bool operator == (EventInfo left, EventInfo right)
162 if ((object)left == (object)right)
164 if ((object)left == null ^ (object)right == null)
166 return left.Equals (right);
169 public static bool operator != (EventInfo left, EventInfo right)
171 if ((object)left == (object)right)
173 if ((object)left == null ^ (object)right == null)
175 return !left.Equals (right);
179 void _EventInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
181 throw new NotImplementedException ();
184 Type _EventInfo.GetType ()
186 // Required or object::GetType becomes virtual final
187 return base.GetType ();
190 void _EventInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
192 throw new NotImplementedException ();
195 void _EventInfo.GetTypeInfoCount (out uint pcTInfo)
197 throw new NotImplementedException ();
200 void _EventInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
202 throw new NotImplementedException ();
206 delegate void AddEventAdapter (object _this, Delegate dele);
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);
215 #pragma warning disable 169
216 // Used via reflection
217 static void AddEventFrame<T,D> (AddEvent<T,D> addEvent, object obj, object dele)
220 throw new TargetException ("Cannot add a handler to a non static event with a null target");
222 throw new TargetException ("Object doesn't match target");
223 addEvent ((T)obj, (D)dele);
226 static void StaticAddEventAdapterFrame<D> (StaticAddEvent<D> addEvent, object obj, object dele)
230 #pragma warning restore 169
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.
236 static AddEventAdapter CreateAddEventDelegate (MethodInfo method)
240 object addHandlerDelegate;
241 MethodInfo adapterFrame;
242 Type addHandlerDelegateType;
245 if (method.IsStatic) {
246 typeVector = new Type[] { method.GetParametersInternal () [0].ParameterType };
247 addHandlerDelegateType = typeof (StaticAddEvent<>);
248 frameName = "StaticAddEventAdapterFrame";
250 typeVector = new Type[] { method.DeclaringType, method.GetParametersInternal () [0].ParameterType };
251 addHandlerDelegateType = typeof (AddEvent<,>);
252 frameName = "AddEventFrame";
255 addHandlerType = addHandlerDelegateType.MakeGenericType (typeVector);
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 ();
264 addHandlerDelegate = Delegate.CreateDelegate (addHandlerType, method);
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);
272 public virtual MethodInfo AddMethod {
273 get { return GetAddMethod (true); }
275 public virtual MethodInfo RaiseMethod {
276 get { return GetRaiseMethod (true); }
278 public virtual MethodInfo RemoveMethod {
279 get { return GetRemoveMethod (true); }
282 [MethodImplAttribute(MethodImplOptions.InternalCall)]
283 private static extern EventInfo internal_from_handle_type (IntPtr event_handle, IntPtr type_handle);
285 internal static EventInfo GetEventFromHandle (Mono.RuntimeEventHandle handle)
287 if (handle.Value == IntPtr.Zero)
288 throw new ArgumentException ("The handle is invalid.");
289 return internal_from_handle_type (handle.Value, IntPtr.Zero);
292 internal static EventInfo GetEventFromHandle (Mono.RuntimeEventHandle handle, RuntimeTypeHandle reflectedType)
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);
298 throw new ArgumentException ("The event handle and the type handle are incompatible.");