/// <summary> /// Adds a handler that is called if the item being matched is of type <paramref name="handlesType"/> /// and <paramref name="shouldHandle"/>, if it has been specified, returns <c>true</c>. /// <remarks>Note that if a previous added handler handled the item, this <paramref name="handler"/> will not be invoked.</remarks> /// </summary> /// <param name="handlesType">The type that it must match in order for <paramref name="handler"/> to be called.</param> /// <param name="handler">The handler that is invoked when everything matches.</param> /// <param name="shouldHandle">An optional predicate to test if the item matches. If it returns <c>true</c> the <paramref name="handler"/> is invoked.</param> /// <exception cref="ArgumentException"> /// This exception is thrown if the given <paramref name="handler"/> cannot handle the given <paramref name="handlesType"/>. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// This exception is thrown if the current state is unknown. /// </exception> /// <exception cref="InvalidOperationException"> /// This exception is thrown if a handler that catches all messages has been added or a partial action has already been built. /// </exception> public void Match(Type handlesType, Action <TItem> handler, Predicate <TItem> shouldHandle = null) { EnsureCanAdd(); EnsureCanHandleType(handlesType); AddHandler(handlesType, PredicateAndHandler.CreateAction(handler, shouldHandle, true)); if (handlesType == _itemType && shouldHandle == null) { _state = State.MatchAnyAdded; } }
/// <summary> /// Adds a handler that is called if the item being matched is of type <typeparamref name="T"/> /// and <paramref name="shouldHandle"/>, if it has been specified, returns <c>true</c>. /// <remarks>Note that if a previous added handler handled the item, this <paramref name="handler"/> will not be invoked.</remarks> /// </summary> /// <typeparam name="T">The type that it must match in order for <paramref name="handler"/> to be called.</typeparam> /// <param name="handler">The handler that is invoked when everything matches.</param> /// <param name="shouldHandle">An optional predicate to test if the item matches. If it returns <c>true</c> the <paramref name="handler"/> is invoked.</param> /// <exception cref="InvalidOperationException"> /// This exception is thrown if a handler that catches all messages has been added or a partial action has already been built. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// This exception is thrown if the current state is unknown. /// </exception> public void Match <T>(Action <T> handler, Predicate <T> shouldHandle = null) where T : TItem { EnsureCanAdd(); var handlesType = typeof(T); AddHandler(handlesType, PredicateAndHandler.CreateAction(handler, shouldHandle)); if (handlesType == _itemType && shouldHandle == null) { _state = State.MatchAnyAdded; } }