// original code from rx.codeplex.com // some modified. /* ------------------ */ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. using System; using System.Collections.Generic; namespace UniRx { /// /// Represents a .NET event invocation consisting of the strongly typed object that raised the event and the data that was generated by the event. /// /// /// The type of the sender that raised the event. /// 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. /// /// /// The type of the event data generated by the event. /// 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. /// public interface IEventPattern { /// /// Gets the sender object that raised the event. /// TSender Sender { get; } /// /// Gets the event data that was generated by the event. /// TEventArgs EventArgs { get; } } /// /// Represents a .NET event invocation consisting of the weakly typed object that raised the event and the data that was generated by the event. /// /// The type of the event data generated by the event. public class EventPattern : EventPattern { /// /// Creates a new data representation instance of a .NET event invocation with the given sender and event data. /// /// The sender object that raised the event. /// The event data that was generated by the event. public EventPattern(object sender, TEventArgs e) : base(sender, e) { } } /// /// Represents a .NET event invocation consisting of the strongly typed object that raised the event and the data that was generated by the event. /// /// The type of the sender that raised the event. /// The type of the event data generated by the event. public class EventPattern : IEquatable>, IEventPattern { /// /// Creates a new data representation instance of a .NET event invocation with the given sender and event data. /// /// The sender object that raised the event. /// The event data that was generated by the event. public EventPattern(TSender sender, TEventArgs e) { Sender = sender; EventArgs = e; } /// /// Gets the sender object that raised the event. /// public TSender Sender { get; private set; } /// /// Gets the event data that was generated by the event. /// public TEventArgs EventArgs { get; private set; } /// /// Determines whether the current EventPattern<TSender, TEventArgs> object represents the same event as a specified EventPattern<TSender, TEventArgs> object. /// /// An object to compare to the current EventPattern<TSender, TEventArgs> object. /// true if both EventPattern<TSender, TEventArgs> objects represent the same event; otherwise, false. public bool Equals(EventPattern other) { if (object.ReferenceEquals(null, other)) return false; if (object.ReferenceEquals(this, other)) return true; return EqualityComparer.Default.Equals(Sender, other.Sender) && EqualityComparer.Default.Equals(EventArgs, other.EventArgs); } /// /// Determines whether the specified System.Object is equal to the current EventPattern<TSender, TEventArgs>. /// /// The System.Object to compare with the current EventPattern<TSender, TEventArgs>. /// true if the specified System.Object is equal to the current EventPattern<TSender, TEventArgs>; otherwise, false. public override bool Equals(object obj) { return Equals(obj as EventPattern); } /// /// Returns the hash code for the current EventPattern<TSender, TEventArgs> instance. /// /// A hash code for the current EventPattern<TSender, TEventArgs> instance. public override int GetHashCode() { var x = EqualityComparer.Default.GetHashCode(Sender); var y = EqualityComparer.Default.GetHashCode(EventArgs); return (x << 5) + (x ^ y); } /// /// Determines whether two specified EventPattern<TSender, TEventArgs> objects represent the same event. /// /// The first EventPattern<TSender, TEventArgs> to compare, or null. /// The second EventPattern<TSender, TEventArgs> to compare, or null. /// true if both EventPattern<TSender, TEventArgs> objects represent the same event; otherwise, false. public static bool operator ==(EventPattern first, EventPattern second) { return object.Equals(first, second); } /// /// Determines whether two specified EventPattern<TSender, TEventArgs> objects represent a different event. /// /// The first EventPattern<TSender, TEventArgs> to compare, or null. /// The second EventPattern<TSender, TEventArgs> to compare, or null. /// true if both EventPattern<TSender, TEventArgs> objects don't represent the same event; otherwise, false. public static bool operator !=(EventPattern first, EventPattern second) { return !object.Equals(first, second); } } }