private MethodInfo GetMethodInfo(DoloresHandler doloresHandler, int numberOfArguments) { string configuredMethod = $"Configured method '{_httpMethodImplementation.MethodName}'"; // At this moment every argument must be of type string. var types = new Type[numberOfArguments]; for (int i = 0; i < numberOfArguments; i++) { types[i] = typeof(string); } MethodInfo method; try { method = doloresHandler.GetType().GetMethod(_httpMethodImplementation.MethodName, types); } catch (Exception exception) { throw new HttpNotImplementedException($"{configuredMethod} is invalid: {exception.Message}", exception); } if (method == null) { throw new HttpNotImplementedException($"{configuredMethod} is not a public instance method on class '{_httpMethodImplementation.FullyQualifiedClassName}' and/or does not have matching arguments"); } return(method); }
private async Task <Response> GetResponseAsync(DoloresHandler doloresHandler) { var methodArguments = ConvertArguments(_request.UriParameters.Values); var methodInfo = GetMethodInfo(doloresHandler, methodArguments.Length); Response response; bool methodIsSync = methodInfo.ReturnType == typeof(Response); bool methodIsAsync = methodInfo.ReturnType == typeof(Task <Response>); if (methodIsSync) { response = (Response)InvokeMethod(doloresHandler, methodInfo, methodArguments); } else if (methodIsAsync) { var result = (Task <Response>)InvokeMethod(doloresHandler, methodInfo, methodArguments); response = await result; } else { throw new HttpNotImplementedException($"Method {methodInfo.Name} must either return type {typeof(Response).Name} or {typeof(Task<Response>).Name}"); } return(response); }
private static object InvokeMethod(DoloresHandler doloresHandler, MethodInfo methodInfo, object[] methodArguments) { try { var result = methodInfo.Invoke(doloresHandler, methodArguments); return(result); } catch (Exception exception) { if (exception.InnerException != null) { throw exception.InnerException; } throw; } }