EventInfo.cs 7.8 KB

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