public static PredicateAndHandler CreateFunc(object func, bool handlerFirstArgumentShouldBeBaseType=false) { var funcHandler = new PredicateAndHandler(HandlerKind.Func, handlerFirstArgumentShouldBeBaseType); funcHandler.Arguments = new[] { new Argument(funcHandler, func, true) }; return funcHandler; }
public static PredicateAndHandler CreateAction(object action, object predicate = null, bool handlerFirstArgumentShouldBeBaseType=false) { if(predicate == null) { var actionHandler = new PredicateAndHandler(HandlerKind.Action, handlerFirstArgumentShouldBeBaseType); actionHandler.Arguments = new[] { new Argument(actionHandler, action, true) }; return actionHandler; } var predicateActionHandler = new PredicateAndHandler(HandlerKind.ActionWithPredicate, handlerFirstArgumentShouldBeBaseType); predicateActionHandler.Arguments = new[] { new Argument(predicateActionHandler, action, true), new Argument(predicateActionHandler, predicate, false) }; return predicateActionHandler; }
private static void AddActionHandlerWithPredicateExpressions(PredicateAndHandler handler, List <Expression> body, Expression argumentExpression, LabelTarget returnTarget) { //Add this code to the body: // if(predicate(arg)) // { // action(arg); // return true; // } body.Add( Expression.IfThen( Expression.Invoke(handler.PredicateExpression, argumentExpression), Expression.Block( Expression.Invoke(handler.ActionOrFuncExpression, argumentExpression), Expression.Return(returnTarget, Expression.Constant(true)) ))); }
/// <summary> /// Adds a handler that is called if the item being matched is of type <paramref name="handlesType"/>. /// The handler should return <c>true</c> if the item sent in matched and was handled. /// <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. It should return <c>true</c> if the item sent in matched and was handled.</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, Func <TItem, bool> handler) { EnsureCanAdd(); EnsureCanHandleType(handlesType); AddHandler(handlesType, PredicateAndHandler.CreateFunc(handler, true)); }