EventInfo.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. #if FEATURE_COMINTEROP
  6. using EventRegistrationToken = System.Runtime.InteropServices.WindowsRuntime.EventRegistrationToken;
  7. #endif //#if FEATURE_COMINTEROP
  8. namespace System.Reflection
  9. {
  10. public abstract class EventInfo : MemberInfo
  11. {
  12. protected EventInfo() { }
  13. public override MemberTypes MemberType => MemberTypes.Event;
  14. public abstract EventAttributes Attributes { get; }
  15. public bool IsSpecialName => (Attributes & EventAttributes.SpecialName) != 0;
  16. public MethodInfo[] GetOtherMethods() => GetOtherMethods(nonPublic: false);
  17. public virtual MethodInfo[] GetOtherMethods(bool nonPublic) { throw NotImplemented.ByDesign; }
  18. public virtual MethodInfo AddMethod => GetAddMethod(nonPublic: true);
  19. public virtual MethodInfo RemoveMethod => GetRemoveMethod(nonPublic: true);
  20. public virtual MethodInfo RaiseMethod => GetRaiseMethod(nonPublic: true);
  21. public MethodInfo GetAddMethod() => GetAddMethod(nonPublic: false);
  22. public MethodInfo GetRemoveMethod() => GetRemoveMethod(nonPublic: false);
  23. public MethodInfo GetRaiseMethod() => GetRaiseMethod(nonPublic: false);
  24. public abstract MethodInfo GetAddMethod(bool nonPublic);
  25. public abstract MethodInfo GetRemoveMethod(bool nonPublic);
  26. public abstract MethodInfo GetRaiseMethod(bool nonPublic);
  27. public virtual bool IsMulticast
  28. {
  29. get
  30. {
  31. Type cl = EventHandlerType;
  32. Type mc = typeof(MulticastDelegate);
  33. return mc.IsAssignableFrom(cl);
  34. }
  35. }
  36. public virtual Type EventHandlerType
  37. {
  38. get
  39. {
  40. MethodInfo m = GetAddMethod(true);
  41. ParameterInfo[] p = m.GetParametersNoCopy();
  42. Type del = typeof(Delegate);
  43. for (int i = 0; i < p.Length; i++)
  44. {
  45. Type c = p[i].ParameterType;
  46. if (c.IsSubclassOf(del))
  47. return c;
  48. }
  49. return null;
  50. }
  51. }
  52. [DebuggerHidden]
  53. [DebuggerStepThrough]
  54. public virtual void AddEventHandler(object target, Delegate handler)
  55. {
  56. MethodInfo addMethod = GetAddMethod(nonPublic: false);
  57. if (addMethod == null)
  58. throw new InvalidOperationException(SR.InvalidOperation_NoPublicAddMethod);
  59. #if FEATURE_COMINTEROP
  60. if (addMethod.ReturnType == typeof(EventRegistrationToken))
  61. throw new InvalidOperationException(SR.InvalidOperation_NotSupportedOnWinRTEvent);
  62. #endif //#if FEATURE_COMINTEROP
  63. addMethod.Invoke(target, new object[] { handler });
  64. }
  65. [DebuggerHidden]
  66. [DebuggerStepThrough]
  67. public virtual void RemoveEventHandler(object target, Delegate handler)
  68. {
  69. MethodInfo removeMethod = GetRemoveMethod(nonPublic: false);
  70. if (removeMethod == null)
  71. throw new InvalidOperationException(SR.InvalidOperation_NoPublicRemoveMethod);
  72. #if FEATURE_COMINTEROP
  73. ParameterInfo[] parameters = removeMethod.GetParametersNoCopy();
  74. if (parameters[0].ParameterType == typeof(EventRegistrationToken))
  75. throw new InvalidOperationException(SR.InvalidOperation_NotSupportedOnWinRTEvent);
  76. #endif //#if FEATURE_COMINTEROP
  77. removeMethod.Invoke(target, new object[] { handler });
  78. }
  79. public override bool Equals(object obj) => base.Equals(obj);
  80. public override int GetHashCode() => base.GetHashCode();
  81. public static bool operator ==(EventInfo left, EventInfo right)
  82. {
  83. if (object.ReferenceEquals(left, right))
  84. return true;
  85. if ((object)left == null || (object)right == null)
  86. return false;
  87. return left.Equals(right);
  88. }
  89. public static bool operator !=(EventInfo left, EventInfo right) => !(left == right);
  90. }
  91. }