EventPattern.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // original code from rx.codeplex.com
  2. // some modified.
  3. /* ------------------ */
  4. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  5. using System;
  6. using System.Collections.Generic;
  7. namespace UniRx
  8. {
  9. /// <summary>
  10. /// Represents a .NET event invocation consisting of the strongly typed object that raised the event and the data that was generated by the event.
  11. /// </summary>
  12. /// <typeparam name="TSender">
  13. /// The type of the sender that raised the event.
  14. /// This type parameter is covariant. That is, you can use either the type you specified or any type that is more derived. For more information about covariance and contravariance, see Covariance and Contravariance in Generics.
  15. /// </typeparam>
  16. /// <typeparam name="TEventArgs">
  17. /// The type of the event data generated by the event.
  18. /// This type parameter is covariant. That is, you can use either the type you specified or any type that is more derived. For more information about covariance and contravariance, see Covariance and Contravariance in Generics.
  19. /// </typeparam>
  20. public interface IEventPattern<TSender, TEventArgs>
  21. {
  22. /// <summary>
  23. /// Gets the sender object that raised the event.
  24. /// </summary>
  25. TSender Sender { get; }
  26. /// <summary>
  27. /// Gets the event data that was generated by the event.
  28. /// </summary>
  29. TEventArgs EventArgs { get; }
  30. }
  31. /// <summary>
  32. /// Represents a .NET event invocation consisting of the weakly typed object that raised the event and the data that was generated by the event.
  33. /// </summary>
  34. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  35. public class EventPattern<TEventArgs> : EventPattern<object, TEventArgs>
  36. {
  37. /// <summary>
  38. /// Creates a new data representation instance of a .NET event invocation with the given sender and event data.
  39. /// </summary>
  40. /// <param name="sender">The sender object that raised the event.</param>
  41. /// <param name="e">The event data that was generated by the event.</param>
  42. public EventPattern(object sender, TEventArgs e)
  43. : base(sender, e)
  44. {
  45. }
  46. }
  47. /// <summary>
  48. /// Represents a .NET event invocation consisting of the strongly typed object that raised the event and the data that was generated by the event.
  49. /// </summary>
  50. /// <typeparam name="TSender">The type of the sender that raised the event.</typeparam>
  51. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  52. public class EventPattern<TSender, TEventArgs> : IEquatable<EventPattern<TSender, TEventArgs>>, IEventPattern<TSender, TEventArgs>
  53. {
  54. /// <summary>
  55. /// Creates a new data representation instance of a .NET event invocation with the given sender and event data.
  56. /// </summary>
  57. /// <param name="sender">The sender object that raised the event.</param>
  58. /// <param name="e">The event data that was generated by the event.</param>
  59. public EventPattern(TSender sender, TEventArgs e)
  60. {
  61. Sender = sender;
  62. EventArgs = e;
  63. }
  64. /// <summary>
  65. /// Gets the sender object that raised the event.
  66. /// </summary>
  67. public TSender Sender { get; private set; }
  68. /// <summary>
  69. /// Gets the event data that was generated by the event.
  70. /// </summary>
  71. public TEventArgs EventArgs { get; private set; }
  72. /// <summary>
  73. /// Determines whether the current EventPattern&lt;TSender, TEventArgs&gt; object represents the same event as a specified EventPattern&lt;TSender, TEventArgs&gt; object.
  74. /// </summary>
  75. /// <param name="other">An object to compare to the current EventPattern&lt;TSender, TEventArgs&gt; object.</param>
  76. /// <returns>true if both EventPattern&lt;TSender, TEventArgs&gt; objects represent the same event; otherwise, false.</returns>
  77. public bool Equals(EventPattern<TSender, TEventArgs> other)
  78. {
  79. if (object.ReferenceEquals(null, other))
  80. return false;
  81. if (object.ReferenceEquals(this, other))
  82. return true;
  83. return EqualityComparer<TSender>.Default.Equals(Sender, other.Sender) && EqualityComparer<TEventArgs>.Default.Equals(EventArgs, other.EventArgs);
  84. }
  85. /// <summary>
  86. /// Determines whether the specified System.Object is equal to the current EventPattern&lt;TSender, TEventArgs&gt;.
  87. /// </summary>
  88. /// <param name="obj">The System.Object to compare with the current EventPattern&lt;TSender, TEventArgs&gt;.</param>
  89. /// <returns>true if the specified System.Object is equal to the current EventPattern&lt;TSender, TEventArgs&gt;; otherwise, false.</returns>
  90. public override bool Equals(object obj)
  91. {
  92. return Equals(obj as EventPattern<TSender, TEventArgs>);
  93. }
  94. /// <summary>
  95. /// Returns the hash code for the current EventPattern&lt;TSender, TEventArgs&gt; instance.
  96. /// </summary>
  97. /// <returns>A hash code for the current EventPattern&lt;TSender, TEventArgs&gt; instance.</returns>
  98. public override int GetHashCode()
  99. {
  100. var x = EqualityComparer<TSender>.Default.GetHashCode(Sender);
  101. var y = EqualityComparer<TEventArgs>.Default.GetHashCode(EventArgs);
  102. return (x << 5) + (x ^ y);
  103. }
  104. /// <summary>
  105. /// Determines whether two specified EventPattern&lt;TSender, TEventArgs&gt; objects represent the same event.
  106. /// </summary>
  107. /// <param name="first">The first EventPattern&lt;TSender, TEventArgs&gt; to compare, or null.</param>
  108. /// <param name="second">The second EventPattern&lt;TSender, TEventArgs&gt; to compare, or null.</param>
  109. /// <returns>true if both EventPattern&lt;TSender, TEventArgs&gt; objects represent the same event; otherwise, false.</returns>
  110. public static bool operator ==(EventPattern<TSender, TEventArgs> first, EventPattern<TSender, TEventArgs> second)
  111. {
  112. return object.Equals(first, second);
  113. }
  114. /// <summary>
  115. /// Determines whether two specified EventPattern&lt;TSender, TEventArgs&gt; objects represent a different event.
  116. /// </summary>
  117. /// <param name="first">The first EventPattern&lt;TSender, TEventArgs&gt; to compare, or null.</param>
  118. /// <param name="second">The second EventPattern&lt;TSender, TEventArgs&gt; to compare, or null.</param>
  119. /// <returns>true if both EventPattern&lt;TSender, TEventArgs&gt; objects don't represent the same event; otherwise, false.</returns>
  120. public static bool operator !=(EventPattern<TSender, TEventArgs> first, EventPattern<TSender, TEventArgs> second)
  121. {
  122. return !object.Equals(first, second);
  123. }
  124. }
  125. }