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.InteropServices;
33 namespace System.Reflection {
36 [ComDefaultInterfaceAttribute (typeof (_EventInfo))]
38 [ClassInterface(ClassInterfaceType.None)]
39 [StructLayout (LayoutKind.Sequential)]
40 public abstract class EventInfo : MemberInfo, _EventInfo {
41 AddEventAdapter cached_add_event;
43 public abstract EventAttributes Attributes {get;}
46 #if NET_4_0 || MOONLIGHT
49 Type EventHandlerType {
52 MethodInfo add = GetAddMethod (true);
53 p = add.GetParameters ();
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");*/
66 #if NET_4_0 || MOONLIGHT
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;}
75 protected EventInfo() {
82 #if NET_4_0 || MOONLIGHT
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 new MethodInfo [0];
133 public MethodInfo[] GetOtherMethods () {
134 return GetOtherMethods (false);
138 [DebuggerStepThrough]
140 #if NET_4_0 || MOONLIGHT
143 void RemoveEventHandler (object target, Delegate handler)
145 MethodInfo remove = GetRemoveMethod ();
147 throw new InvalidOperationException ("Cannot remove a handler to an event that doesn't have a visible remove method");
149 remove.Invoke (target, new object [] {handler});
153 public override bool Equals (object obj)
155 return obj == (object) this;
158 public override int GetHashCode ()
160 return base.GetHashCode ();
163 public static bool operator == (EventInfo left, EventInfo right)
165 if ((object)left == (object)right)
167 if ((object)left == null ^ (object)right == null)
169 return left.Equals (right);
172 public static bool operator != (EventInfo left, EventInfo right)
174 if ((object)left == (object)right)
176 if ((object)left == null ^ (object)right == null)
178 return !left.Equals (right);
182 void _EventInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
184 throw new NotImplementedException ();
187 void _EventInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
189 throw new NotImplementedException ();
192 void _EventInfo.GetTypeInfoCount (out uint pcTInfo)
194 throw new NotImplementedException ();
197 void _EventInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
199 throw new NotImplementedException ();
202 delegate void AddEventAdapter (object _this, Delegate dele);
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);
211 #pragma warning disable 169
212 // Used via reflection
213 static void AddEventFrame<T,D> (AddEvent<T,D> addEvent, object obj, object dele)
216 throw new TargetException ("Cannot add a handler to a non static event with a null target");
218 throw new TargetException ("Object doesn't match target");
219 addEvent ((T)obj, (D)dele);
222 static void StaticAddEventAdapterFrame<D> (StaticAddEvent<D> addEvent, object obj, object dele)
226 #pragma warning restore 169
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.
232 static AddEventAdapter CreateAddEventDelegate (MethodInfo method)
236 object addHandlerDelegate;
237 MethodInfo adapterFrame;
238 Type addHandlerDelegateType;
241 if (method.IsStatic) {
242 typeVector = new Type[] { method.GetParameters () [0].ParameterType };
243 addHandlerDelegateType = typeof (StaticAddEvent<>);
244 frameName = "StaticAddEventAdapterFrame";
246 typeVector = new Type[] { method.DeclaringType, method.GetParameters () [0].ParameterType };
247 addHandlerDelegateType = typeof (AddEvent<,>);
248 frameName = "AddEventFrame";
251 addHandlerType = addHandlerDelegateType.MakeGenericType (typeVector);
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 ();
260 addHandlerDelegate = Delegate.CreateDelegate (addHandlerType, method);
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);