/// <summary>
        /// Adds parameter to <see cref="BlockExpression"/>
        /// </summary>
        public BlockBuilder Parameter <T>(out ExpressionContainer <T> parameter, T value)
        {
            var expression = Expression.Parameter(typeof(T));

            parameter = ExpressionShortcuts.Arg <T>(expression);
            return(Parameter(parameter, ExpressionShortcuts.Arg(value)));
        }
        /// <summary>
        /// Adds parameter to <see cref="BlockExpression"/>
        /// </summary>
        public BlockBuilder Parameter <T>(string name, out ExpressionContainer <T> parameter, ExpressionContainer <T> value)
        {
            var expression = Expression.Parameter(typeof(T), name);

            parameter = ExpressionShortcuts.Arg <T>(expression);
            return(Parameter(parameter, value));
        }
Beispiel #3
0
        /// <summary>
        /// Adds <see langword="finally" /> statement
        /// </summary>
        /// <param name="finally"><see langword="catch" /> block body</param>
        /// <returns></returns>
        public TryCatchFinallyBuilder Finally(Action <BlockBuilder> @finally)
        {
            var blockBuilder = ExpressionShortcuts.Block();

            @finally(blockBuilder);

            return(Finally(blockBuilder));
        }
Beispiel #4
0
        /// <summary>
        /// Adds <see langword="catch" /> statement catching <see cref="Exception"/> specified as <typeparamref name="T"/>
        /// </summary>
        /// <param name="catch"><see langword="catch" /> block body</param>
        /// <typeparam name="T"><see cref="Exception"/> type to catch</typeparam>
        /// <returns></returns>
        public TryCatchFinallyBuilder Catch <T>(Func <ExpressionContainer <T>, Expression> @catch) where T : Exception
        {
            var exception = ExpressionShortcuts.Var <T>();
            var body      = @catch(exception);

            var catchBlock = Expression.Catch((ParameterExpression)exception, body);

            return(Catch(catchBlock));
        }
Beispiel #5
0
        /// <summary>
        /// Adds <see langword="catch" /> statement catching all <see cref="Exception"/> types
        /// </summary>
        /// <param name="catch"></param>
        /// <param name="when"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public TryCatchFinallyBuilder Catch <T>(Func <ExpressionContainer <T>, Expression> @catch, Func <ExpressionContainer <T>, ExpressionContainer <bool> > when) where T : Exception
        {
            var exception = ExpressionShortcuts.Var <T>();
            var body      = @catch(exception);

            var filter     = when?.Invoke(exception);
            var catchBlock = filter == null
                ? Expression.Catch((ParameterExpression)exception, body)
                : Expression.Catch((ParameterExpression)exception, body, filter);

            return(Catch(catchBlock));
        }
        /// <summary>
        /// Creates <see cref="InvocationExpression"/> out of current <see cref="BlockExpression"/>.
        /// </summary>
        public ExpressionContainer <T> Invoke <T>(params ExpressionContainer[] parameters)
        {
            var lambda = Expression.Lambda(Expression, parameters.Select(o => (ParameterExpression)o.Expression));

            return(ExpressionShortcuts.Arg <T>(Expression.Invoke(lambda)));
        }