EventPattern.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. namespace System.Reactive
  6. {
  7. /// <summary>
  8. /// Represents a .NET event invocation consisting of the weakly typed object that raised the event and the data that was generated by the event.
  9. /// </summary>
  10. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  11. public class EventPattern<TEventArgs> : EventPattern<object, TEventArgs>
  12. {
  13. /// <summary>
  14. /// Creates a new data representation instance of a .NET event invocation with the given sender and event data.
  15. /// </summary>
  16. /// <param name="sender">The sender object that raised the event.</param>
  17. /// <param name="e">The event data that was generated by the event.</param>
  18. public EventPattern(object sender, TEventArgs e)
  19. : base(sender, e)
  20. {
  21. }
  22. }
  23. /// <summary>
  24. /// Represents a .NET event invocation consisting of the strongly typed object that raised the event and the data that was generated by the event.
  25. /// </summary>
  26. /// <typeparam name="TSender">The type of the sender that raised the event.</typeparam>
  27. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  28. public class EventPattern<TSender, TEventArgs> : IEquatable<EventPattern<TSender, TEventArgs>>, IEventPattern<TSender, TEventArgs>
  29. {
  30. /// <summary>
  31. /// Creates a new data representation instance of a .NET event invocation with the given sender and event data.
  32. /// </summary>
  33. /// <param name="sender">The sender object that raised the event.</param>
  34. /// <param name="e">The event data that was generated by the event.</param>
  35. public EventPattern(TSender sender, TEventArgs e)
  36. {
  37. Sender = sender;
  38. EventArgs = e;
  39. }
  40. /// <summary>
  41. /// Gets the sender object that raised the event.
  42. /// </summary>
  43. public TSender Sender { get; }
  44. /// <summary>
  45. /// Gets the event data that was generated by the event.
  46. /// </summary>
  47. public TEventArgs EventArgs { get; }
  48. /// <summary>
  49. /// Determines whether the current <see cref="EventPattern{TSender, TEventArgs}"/> object represents the same event as a specified <see cref="EventPattern{TSender, TEventArgs}"/> object.
  50. /// </summary>
  51. /// <param name="other">An object to compare to the current <see cref="EventPattern{TSender, TEventArgs}"/> object.</param>
  52. /// <returns><c>true</c> if both <see cref="EventPattern{TSender, TEventArgs}"/> objects represent the same event; otherwise, <c>false</c>.</returns>
  53. public bool Equals(EventPattern<TSender, TEventArgs> other)
  54. {
  55. if (ReferenceEquals(null, other))
  56. return false;
  57. if (ReferenceEquals(this, other))
  58. return true;
  59. return EqualityComparer<TSender>.Default.Equals(Sender, other.Sender) && EqualityComparer<TEventArgs>.Default.Equals(EventArgs, other.EventArgs);
  60. }
  61. /// <summary>
  62. /// Determines whether the specified System.Object is equal to the current <see cref="EventPattern{TSender, TEventArgs}"/>.
  63. /// </summary>
  64. /// <param name="obj">The System.Object to compare with the current <see cref="EventPattern{TSender, TEventArgs}"/>.</param>
  65. /// <returns><c>true</c> if the specified System.Object is equal to the current <see cref="EventPattern{TSender, TEventArgs}"/>; otherwise, <c>false</c>.</returns>
  66. public override bool Equals(object obj) => Equals(obj as EventPattern<TSender, TEventArgs>);
  67. /// <summary>
  68. /// Returns the hash code for the current <see cref="EventPattern{TSender, TEventArgs}"/> instance.
  69. /// </summary>
  70. /// <returns>A hash code for the current <see cref="EventPattern{TSender, TEventArgs}"/> instance.</returns>
  71. public override int GetHashCode()
  72. {
  73. var x = EqualityComparer<TSender>.Default.GetHashCode(Sender);
  74. var y = EqualityComparer<TEventArgs>.Default.GetHashCode(EventArgs);
  75. return (x << 5) + (x ^ y);
  76. }
  77. /// <summary>
  78. /// Determines whether two specified <see cref="EventPattern{TSender, TEventArgs}"/> objects represent the same event.
  79. /// </summary>
  80. /// <param name="first">The first <see cref="EventPattern{TSender, TEventArgs}"/> to compare, or <c>null</c>.</param>
  81. /// <param name="second">The second <see cref="EventPattern{TSender, TEventArgs}"/> to compare, or <c>null</c>.</param>
  82. /// <returns><c>true</c> if both <see cref="EventPattern{TSender, TEventArgs}"/> objects represent the same event; otherwise, <c>false</c>.</returns>
  83. public static bool operator ==(EventPattern<TSender, TEventArgs> first, EventPattern<TSender, TEventArgs> second) => Equals(first, second);
  84. /// <summary>
  85. /// Determines whether two specified <see cref="EventPattern{TSender, TEventArgs}"/> objects represent a different event.
  86. /// </summary>
  87. /// <param name="first">The first <see cref="EventPattern{TSender, TEventArgs}"/> to compare, or <c>null</c>.</param>
  88. /// <param name="second">The second <see cref="EventPattern{TSender, TEventArgs}"/> to compare, or <c>null</c>.</param>
  89. /// <returns><c>true</c> if both <see cref="EventPattern{TSender, TEventArgs}"/> objects don't represent the same event; otherwise, <c>false</c>.</returns>
  90. public static bool operator !=(EventPattern<TSender, TEventArgs> first, EventPattern<TSender, TEventArgs> second) => !Equals(first, second);
  91. }
  92. }