EventInfo.cs 7.6 KB

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