2
0

EventInfo.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.CompilerServices;
  31. using System.Runtime.InteropServices;
  32. namespace System.Reflection {
  33. [Serializable]
  34. public abstract partial class EventInfo : MemberInfo {
  35. AddEventAdapter cached_add_event;
  36. [DebuggerHidden]
  37. [DebuggerStepThrough]
  38. public virtual void AddEventHandler (object target, Delegate handler)
  39. {
  40. // this optimization cause problems with full AOT
  41. // see bug https://bugzilla.xamarin.com/show_bug.cgi?id=3682
  42. #if FULL_AOT_RUNTIME
  43. MethodInfo add = GetAddMethod ();
  44. if (add == null)
  45. throw new InvalidOperationException (SR.InvalidOperation_NoPublicAddMethod);
  46. if (target == null && !add.IsStatic)
  47. throw new TargetException ("Cannot add a handler to a non static event with a null target");
  48. add.Invoke (target, new object [] {handler});
  49. #else
  50. if (cached_add_event == null) {
  51. MethodInfo add = GetAddMethod ();
  52. if (add == null)
  53. throw new InvalidOperationException (SR.InvalidOperation_NoPublicAddMethod);
  54. if (add.DeclaringType.IsValueType) {
  55. if (target == null && !add.IsStatic)
  56. throw new TargetException ("Cannot add a handler to a non static event with a null target");
  57. add.Invoke (target, new object [] {handler});
  58. return;
  59. }
  60. cached_add_event = CreateAddEventDelegate (add);
  61. }
  62. //if (target == null && is_instance)
  63. // throw new TargetException ("Cannot add a handler to a non static event with a null target");
  64. cached_add_event (target, handler);
  65. #endif
  66. }
  67. delegate void AddEventAdapter (object _this, Delegate dele);
  68. // this optimization cause problems with full AOT
  69. // see bug https://bugzilla.xamarin.com/show_bug.cgi?id=3682
  70. // do not revove the above delegate or it's field since it's required by the runtime!
  71. #if !FULL_AOT_RUNTIME
  72. delegate void AddEvent<T, D> (T _this, D dele);
  73. delegate void StaticAddEvent<D> (D dele);
  74. #pragma warning disable 169
  75. // Used via reflection
  76. static void AddEventFrame<T,D> (AddEvent<T,D> addEvent, object obj, object dele)
  77. {
  78. if (obj == null)
  79. throw new TargetException ("Cannot add a handler to a non static event with a null target");
  80. if (!(obj is T))
  81. throw new TargetException ("Object doesn't match target");
  82. if (!(dele is D))
  83. throw new ArgumentException ($"Object of type {dele.GetType ()} cannot be converted to type {typeof (D)}.");
  84. addEvent ((T)obj, (D)dele);
  85. }
  86. static void StaticAddEventAdapterFrame<D> (StaticAddEvent<D> addEvent, object obj, object dele)
  87. {
  88. addEvent ((D)dele);
  89. }
  90. #pragma warning restore 169
  91. /*
  92. * The idea behing this optimization is to use a pair of delegates to simulate the same effect of doing a reflection call.
  93. * The first delegate performs casting and boxing, the second dispatch to the add_... method.
  94. */
  95. static AddEventAdapter CreateAddEventDelegate (MethodInfo method)
  96. {
  97. Type[] typeVector;
  98. Type addHandlerType;
  99. object addHandlerDelegate;
  100. MethodInfo adapterFrame;
  101. Type addHandlerDelegateType;
  102. string frameName;
  103. if (method.IsStatic) {
  104. typeVector = new Type[] { method.GetParametersInternal () [0].ParameterType };
  105. addHandlerDelegateType = typeof (StaticAddEvent<>);
  106. frameName = "StaticAddEventAdapterFrame";
  107. } else {
  108. typeVector = new Type[] { method.DeclaringType, method.GetParametersInternal () [0].ParameterType };
  109. addHandlerDelegateType = typeof (AddEvent<,>);
  110. frameName = "AddEventFrame";
  111. }
  112. addHandlerType = addHandlerDelegateType.MakeGenericType (typeVector);
  113. #if MOBILE
  114. // with Silverlight a coreclr failure (e.g. Transparent caller creating a delegate on a Critical method)
  115. // would normally throw an ArgumentException, so we set throwOnBindFailure to false and check for a null
  116. // delegate that we can transform into a MethodAccessException
  117. addHandlerDelegate = Delegate.CreateDelegate (addHandlerType, method, false);
  118. if (addHandlerDelegate == null)
  119. throw new MethodAccessException ();
  120. #else
  121. addHandlerDelegate = Delegate.CreateDelegate (addHandlerType, method);
  122. #endif
  123. adapterFrame = typeof (EventInfo).GetMethod (frameName, BindingFlags.Static | BindingFlags.NonPublic);
  124. adapterFrame = adapterFrame.MakeGenericMethod (typeVector);
  125. return (AddEventAdapter)Delegate.CreateDelegate (typeof (AddEventAdapter), addHandlerDelegate, adapterFrame, true);
  126. }
  127. #endif
  128. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  129. static extern EventInfo internal_from_handle_type (IntPtr event_handle, IntPtr type_handle);
  130. internal static EventInfo GetEventFromHandle (Mono.RuntimeEventHandle handle, RuntimeTypeHandle reflectedType)
  131. {
  132. if (handle.Value == IntPtr.Zero)
  133. throw new ArgumentException ("The handle is invalid.");
  134. EventInfo ei = internal_from_handle_type (handle.Value, reflectedType.Value);
  135. if (ei == null)
  136. throw new ArgumentException ("The event handle and the type handle are incompatible.");
  137. return ei;
  138. }
  139. }
  140. }