Ejemplo n.º 1
0
        /// <summary>
        /// 執行有回傳值的method
        /// </summary>
        /// <typeparam name="TResult">回傳值的型別</typeparam>
        /// <param name="fun">欲執行的委派</param>
        /// <returns>執行結果</returns>
        public static ExecutedResult <TResult> Excute <TResult>(Expression <Func <TResult> > fun)
        {
            MethodCallExpression method = fun.Body as MethodCallExpression;
            string log = GetExpressionLog(method);

            DateTime start  = DateTime.Now;
            TResult  result = fun.Compile()();

            LogPerformance(DateTime.Now - start, log);

            ExecutedResult <TResult> executedResult = new ExecutedResult <TResult> {
                IsSuccess = true, Result = result
            };

            return(executedResult);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 嘗試執行有回傳值的method, 若執行失敗會記錄log
        /// </summary>
        /// <typeparam name="TResult">回傳值的型別</typeparam>
        /// <param name="fun">欲執行的委派</param>
        /// <returns>執行結果</returns>
        public static ExecutedResult <TResult> TryExcute <TResult>(Expression <Func <TResult> > fun)
        {
            ExecutedResult <TResult> result;

            try
            {
                result = Excute(fun);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
                result = new ExecutedResult <TResult> {
                    IsSuccess = false, Exception = ex
                };
            }

            return(result);
        }