示例#1
0
        /// <summary>
        ///     Executes the specified formatter.
        /// </summary>
        public virtual ObjectPromise Execute(
            FormatterCache formatter,
            object sourceValue,
            ParserOptions parserOptions,
            FormatterArgumentType[] args)
        {
            Log(parserOptions, () => $"Execute the formatter {formatter.Model.Name} with arguments",
                () => args.ToDictionary(e => e.Name, e => (object)e));

            var mappedValues = formatter.ValueBuffer;

            var i = 0;

            foreach (var formatterArgumentMap in formatter.TestedTypes.Arguments)
            {
                var valueObtainValue = formatterArgumentMap.Value.ObtainValue(sourceValue, args);
                if (formatterArgumentMap.Value.ConverterFunc != null)
                {
                    var convValue = formatterArgumentMap.Value.ConverterFunc(valueObtainValue);
                    valueObtainValue = convValue ?? valueObtainValue;
                }
                mappedValues[i++] = valueObtainValue;
            }

            try
            {
                var functionTarget = formatter.Model.FunctionTarget;
                var method         = formatter.TestedTypes.PrepareInvoke(mappedValues);

                if (formatter.Model.LinkFunctionTarget && !method.IsStatic &&
                    method.DeclaringType == sourceValue.GetType())
                {
                    functionTarget = sourceValue;
                }

                var taskAlike = method.Invoke(functionTarget, mappedValues);
                return(taskAlike.UnpackFormatterTask());
            }
            catch (Exception e)
            {
                if (ExceptionHandling == FormatterServiceExceptionHandling.IgnoreSilently)
                {
                    return(AsyncHelper.EmptyPromise <object>());
                }

                if (ExceptionHandling == FormatterServiceExceptionHandling.PrintExceptions)
                {
                    return((e.ToString() as object).ToPromise());
                }

                throw;
            }
        }