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)]
41 public abstract class EventInfo : MemberInfo {
43 public abstract class EventInfo : MemberInfo, _EventInfo {
45 AddEventAdapter cached_add_event;
47 public abstract EventAttributes Attributes {get;}
51 Type EventHandlerType {
54 MethodInfo add = GetAddMethod (true);
55 p = add.GetParametersInternal ();
57 Type t = p [0].ParameterType;
58 /* is it alwasys the first arg?
59 if (!t.IsSubclassOf (typeof (System.Delegate)))
60 throw new Exception ("no delegate in event");*/
70 bool IsMulticast {get {return true;}}
71 public bool IsSpecialName {get {return (Attributes & EventAttributes.SpecialName ) != 0;}}
72 public override MemberTypes MemberType {
73 get {return MemberTypes.Event;}
76 protected EventInfo() {
84 void AddEventHandler (object target, Delegate handler)
86 // this optimization cause problems with full AOT
87 // see bug https://bugzilla.xamarin.com/show_bug.cgi?id=3682
89 MethodInfo add = GetAddMethod ();
91 throw new InvalidOperationException ("Cannot add a handler to an event that doesn't have a visible add method");
92 if (target == null && !add.IsStatic)
93 throw new TargetException ("Cannot add a handler to a non static event with a null target");
94 add.Invoke (target, new object [] {handler});
96 if (cached_add_event == null) {
97 MethodInfo add = GetAddMethod ();
99 throw new InvalidOperationException ("Cannot add a handler to an event that doesn't have a visible add method");
100 if (add.DeclaringType.IsValueType) {
101 if (target == null && !add.IsStatic)
102 throw new TargetException ("Cannot add a handler to a non static event with a null target");
103 add.Invoke (target, new object [] {handler});
106 cached_add_event = CreateAddEventDelegate (add);
108 //if (target == null && is_instance)
109 // throw new TargetException ("Cannot add a handler to a non static event with a null target");
110 cached_add_event (target, handler);
114 public MethodInfo GetAddMethod() {
115 return GetAddMethod (false);
117 public abstract MethodInfo GetAddMethod(bool nonPublic);
118 public MethodInfo GetRaiseMethod() {
119 return GetRaiseMethod (false);
121 public abstract MethodInfo GetRaiseMethod( bool nonPublic);
122 public MethodInfo GetRemoveMethod() {
123 return GetRemoveMethod (false);
125 public abstract MethodInfo GetRemoveMethod( bool nonPublic);
127 public virtual MethodInfo[] GetOtherMethods (bool nonPublic) {
128 // implemented by the derived class
129 return EmptyArray<MethodInfo>.Value;
132 public MethodInfo[] GetOtherMethods () {
133 return GetOtherMethods (false);
137 [DebuggerStepThrough]
140 void RemoveEventHandler (object target, Delegate handler)
142 MethodInfo remove = GetRemoveMethod ();
144 throw new InvalidOperationException ("Cannot remove a handler to an event that doesn't have a visible remove method");
146 remove.Invoke (target, new object [] {handler});
149 public override bool Equals (object obj)
151 return obj == (object) this;
154 public override int GetHashCode ()
156 return base.GetHashCode ();
159 public static bool operator == (EventInfo left, EventInfo right)
161 if ((object)left == (object)right)
163 if ((object)left == null ^ (object)right == null)
165 return left.Equals (right);
168 public static bool operator != (EventInfo left, EventInfo right)
170 if ((object)left == (object)right)
172 if ((object)left == null ^ (object)right == null)
174 return !left.Equals (right);
178 void _EventInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
180 throw new NotImplementedException ();
183 Type _EventInfo.GetType ()
185 // Required or object::GetType becomes virtual final
186 return base.GetType ();
189 void _EventInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
191 throw new NotImplementedException ();
194 void _EventInfo.GetTypeInfoCount (out uint pcTInfo)
196 throw new NotImplementedException ();
199 void _EventInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
201 throw new NotImplementedException ();
205 delegate void AddEventAdapter (object _this, Delegate dele);
207 // this optimization cause problems with full AOT
208 // see bug https://bugzilla.xamarin.com/show_bug.cgi?id=3682
209 // do not revove the above delegate or it's field since it's required by the runtime!
210 #if !FULL_AOT_RUNTIME
211 delegate void AddEvent<T, D> (T _this, D dele);
212 delegate void StaticAddEvent<D> (D dele);
214 #pragma warning disable 169
215 // Used via reflection
216 static void AddEventFrame<T,D> (AddEvent<T,D> addEvent, object obj, object dele)
219 throw new TargetException ("Cannot add a handler to a non static event with a null target");
221 throw new TargetException ("Object doesn't match target");
222 addEvent ((T)obj, (D)dele);
225 static void StaticAddEventAdapterFrame<D> (StaticAddEvent<D> addEvent, object obj, object dele)
229 #pragma warning restore 169
232 * The idea behing this optimization is to use a pair of delegates to simulate the same effect of doing a reflection call.
233 * The first delegate performs casting and boxing, the second dispatch to the add_... method.
235 static AddEventAdapter CreateAddEventDelegate (MethodInfo method)
239 object addHandlerDelegate;
240 MethodInfo adapterFrame;
241 Type addHandlerDelegateType;
244 if (method.IsStatic) {
245 typeVector = new Type[] { method.GetParametersInternal () [0].ParameterType };
246 addHandlerDelegateType = typeof (StaticAddEvent<>);
247 frameName = "StaticAddEventAdapterFrame";
249 typeVector = new Type[] { method.DeclaringType, method.GetParametersInternal () [0].ParameterType };
250 addHandlerDelegateType = typeof (AddEvent<,>);
251 frameName = "AddEventFrame";
254 addHandlerType = addHandlerDelegateType.MakeGenericType (typeVector);
256 // with Silverlight a coreclr failure (e.g. Transparent caller creating a delegate on a Critical method)
257 // would normally throw an ArgumentException, so we set throwOnBindFailure to false and check for a null
258 // delegate that we can transform into a MethodAccessException
259 addHandlerDelegate = Delegate.CreateDelegate (addHandlerType, method, false);
260 if (addHandlerDelegate == null)
261 throw new MethodAccessException ();
263 addHandlerDelegate = Delegate.CreateDelegate (addHandlerType, method);
265 adapterFrame = typeof (EventInfo).GetMethod (frameName, BindingFlags.Static | BindingFlags.NonPublic);
266 adapterFrame = adapterFrame.MakeGenericMethod (typeVector);
267 return (AddEventAdapter)Delegate.CreateDelegate (typeof (AddEventAdapter), addHandlerDelegate, adapterFrame, true);
271 public virtual MethodInfo AddMethod {
272 get { return GetAddMethod (true); }
274 public virtual MethodInfo RaiseMethod {
275 get { return GetRaiseMethod (true); }
277 public virtual MethodInfo RemoveMethod {
278 get { return GetRemoveMethod (true); }