Beispiel #1
0
        /// <summary>
        /// Executes the specified expression.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="action">The action.</param>
        /// <returns></returns>
        protected T Execute <T>(Func <T> action)
        {
            DateTime before = DateTime.UtcNow;

#if !NETFX_CORE && !WP
            this.Log.Debug($"Executing action '{action.Method().Name}'");
#endif
            try
            {
                T        result    = action();
                TimeSpan timeTaken = DateTime.UtcNow - before;
#if !NETFX_CORE && !WP
                this.Log.Debug($"Action '{action.Method().Name}' executed. Took {timeTaken.TotalMilliseconds} ms.");
#endif
                return(result);
            }
            catch (Exception ex)
            {
#if !NETFX_CORE && !WP
                this.Log.Error($"There was an error executing Action '{action.Method().Name}'. Message: {ex.Message}");
#endif
                throw;
            }
        }
        private static object EvaluateUnaryOperation(object argument, Func <Expression, Expression> expressionBuilderMethod, Type resultType)
        {
            string cacheKey = expressionBuilderMethod.Method().Name + "_" + resultType.Name;
            Func <object, object> lambda;

            lock (unaryDelegateCache)
            {
                if (!unaryDelegateCache.TryGetValue(cacheKey, out lambda))
                {
                    lambda = GetUnaryEvaluationDelegate(expressionBuilderMethod, resultType);
                    unaryDelegateCache.Add(cacheKey, lambda);
                }
            }

            argument = ChangeType(argument, resultType);

            return(lambda(argument));
        }
        /// <summary>
        /// Applies given arithmetic operation to two arguments and returns the result of the operation.
        /// </summary>
        /// <param name="leftArgument">First argument to the operation.</param>
        /// <param name="rightArgument">Second argument to the operation.</param>
        /// <param name="expressionBuilderMethod">Linq Expression builder method that describes the operation.</param>
        /// <param name="commonType">Common CLR type for both input types (will be the type of the result of the operation)</param>
        /// <returns>Result of arithmetic operation.</returns>
        private static object EvaluateBinaryOperation(object leftArgument, object rightArgument, Func <Expression, Expression, Expression> expressionBuilderMethod, Type commonType)
        {
            string cacheKey = expressionBuilderMethod.Method().Name + "_" + commonType.Name;
            Func <object, object, object> lambda;

            lock (binaryDelegateCache)
            {
                if (!binaryDelegateCache.TryGetValue(cacheKey, out lambda))
                {
                    lambda = GetBinaryEvaluationDelegate(expressionBuilderMethod, commonType);
                    binaryDelegateCache.Add(cacheKey, lambda);
                }
            }

            leftArgument  = ChangeType(leftArgument, commonType);
            rightArgument = ChangeType(rightArgument, commonType);

            return(lambda(leftArgument, rightArgument));
        }
        private static object EvaluateUnaryOperation(object argument, Func<Expression, Expression> expressionBuilderMethod, Type resultType)
        {
            string cacheKey = expressionBuilderMethod.Method().Name + "_" + resultType.Name;
            Func<object, object> lambda;

            lock (unaryDelegateCache)
            {
                if (!unaryDelegateCache.TryGetValue(cacheKey, out lambda))
                {
                    lambda = GetUnaryEvaluationDelegate(expressionBuilderMethod, resultType);
                    unaryDelegateCache.Add(cacheKey, lambda);
                }
            }

            argument = ChangeType(argument, resultType);

            return lambda(argument);
        }
        /// <summary>
        /// Applies given arithmetic operation to two arguments and returns the result of the operation.
        /// </summary>
        /// <param name="leftArgument">First argument to the operation.</param>
        /// <param name="rightArgument">Second argument to the operation.</param>
        /// <param name="expressionBuilderMethod">Linq Expression builder method that describes the operation.</param>
        /// <param name="commonType">Common CLR type for both input types (will be the type of the result of the operation)</param>
        /// <returns>Result of arithmetic operation.</returns>
        private static object EvaluateBinaryOperation(object leftArgument, object rightArgument, Func<Expression, Expression, Expression> expressionBuilderMethod, Type commonType)
        {
            string cacheKey = expressionBuilderMethod.Method().Name + "_" + commonType.Name;
            Func<object, object, object> lambda;

            lock (binaryDelegateCache)
            {
                if (!binaryDelegateCache.TryGetValue(cacheKey, out lambda))
                {
                    lambda = GetBinaryEvaluationDelegate(expressionBuilderMethod, commonType);
                    binaryDelegateCache.Add(cacheKey, lambda);
                }
            }

            leftArgument = ChangeType(leftArgument, commonType);
            rightArgument = ChangeType(rightArgument, commonType);

            return lambda(leftArgument, rightArgument);
        }