EventInfo.cs 8.9 KB

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