private Task <IActionResult> ToActionResultAsync(object returnValue, Type returnType, IActionResultTypeMapper mapper)
        {
            //Null
            if (returnValue == null || returnType == typeof(Task) || returnType == typeof(ValueTask))
            {
                return(Task.FromResult <IActionResult>(NullActionResult.Instance));
            }

            //IActionResult
            if (returnValue is IActionResult actionResult)
            {
                return(Task.FromResult(actionResult));
            }

            //Task<TResult>
            if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task <>))
            {
                var declaredType = returnType.GenericTypeArguments.Single();
                var taskOfResult = _taskConvertMethod.MakeGenericMethod(declaredType).Invoke(null, new object[] { returnValue, mapper });
                return((Task <IActionResult>)taskOfResult);
            }

            //ValueTask<TResult>
            if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(ValueTask <>))
            {
                var declaredType      = returnType.GenericTypeArguments.Single();
                var valueTaskOfResult = _valueTaskConvertMethod.MakeGenericMethod(declaredType).Invoke(null, new object[] { returnValue, mapper });
                return((Task <IActionResult>)valueTaskOfResult);
            }

            return(Task.FromResult(mapper.Convert(returnValue, returnType)));
        }
        private static async Task <IActionResult> ConvertFromValueTaskAsync <TValue>(ValueTask <TValue> returnValue, IActionResultTypeMapper mapper)
        {
            var result = await returnValue;

            return(result is IActionResult actionResult
            ? actionResult
            : mapper.Convert(result, typeof(TValue)));
        }
        private IActionResult ConvertToActionResult(IActionResultTypeMapper mapper, object returnValue, Type declaredType)
        {
            var result = (returnValue as IActionResult) ?? mapper.Convert(returnValue, declaredType);

            if (result == null)
            {
                throw new InvalidOperationException(Resources.FormatActionResult_ActionReturnValueCannotBeNull(declaredType));
            }

            return(result);
        }
예제 #4
0
        private IActionResult ConvertToActionResult(IActionResultTypeMapper mapper, object returnValue, Type declaredType)
        {
            var result = (returnValue as IActionResult) ?? mapper.Convert(returnValue, declaredType);

            if (result == null)
            {
                throw new InvalidOperationException();
            }

            return(result);
        }