コード例 #1
0
 private MethodInfo GetRunMethod(string httpMethod)
 {
     return(_handlerType.GetInterfaces()
            .Where(HttpMethodAttribute.IsAppliedTo)
            .Select(t => HttpMethodAttribute.GetMethod(t, httpMethod))
            .Single(a => a != null));
 }
コード例 #2
0
        private Expression BuildAsyncRunBlock()
        {
            var run        = HttpMethodAttribute.GetMethod(_type, _httpMethod);
            var parameters = run.GetParameters();

            if (parameters.Length == 0)
            {
                return(Expression.Assign(_task, Expression.Call(_handler, run)));
            }
            var getInput          = typeof(GetInput).GetMethod("Impl", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(parameters[0].ParameterType);
            var checkRunException = typeof(CheckRunException).GetMethod("ImplAsync",
                                                                        BindingFlags.Public | BindingFlags.Static);

            return(Expression.Assign(_task, Expression.Call(checkRunException, Expression.Call(_handler, run, Expression.Call(getInput, _context)), _context)));
        }
コード例 #3
0
        private Expression BuildRunBlock()
        {
            var run        = HttpMethodAttribute.GetMethod(_type, _httpMethod);
            var parameters = run.GetParameters();

            if (parameters.Length == 0)
            {
                return(Expression.Assign(_status, Expression.Call(_handler, run)));
            }

            var methodType = HttpMethodAttribute.GetAttributedType(_type, _httpMethod);

            var genericType = methodType.GetGenericArguments().Single();
            var getInput    = typeof(GetInput).GetMethod("Impl", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(genericType);

            return(Expression.Assign(_status, Expression.Call(_handler, run, Expression.Call(getInput, _context))));
        }
コード例 #4
0
        Method GetMethod(HttpMethodAttribute attrib, MethodInfo methodInfo)
        {
            string actionTemplate = attrib.Template;

            if (string.IsNullOrEmpty(actionTemplate))
            {
                actionTemplate = methodInfo.GetCustomAttribute <RouteAttribute>()?.Template;
            }
            Method method = convertMethod.Convert(methodInfo);

            if (!method.ReturnType.IsAsync)
            {
                method.ReturnType = DotNetType.MakeAsync(method.ReturnType);
            }
            method.Body = new CodeBlock();
            StringBuilder sb = new StringBuilder();

            using (StringWriter writer = new StringWriter(sb)) {
                writer.Write("string path = $\"{ControllerPath}");
                if (!string.IsNullOrEmpty(actionTemplate))
                {
                    writer.Write("/");
                    writer.Write(actionTemplate);
                }
                writer.WriteLine("\";");
                writer.WriteLine("var queryString = new System.Collections.Specialized.NameValueCollection();");
                HashSet <string> actionRoutes = new HashSet <string>();
                if (attrib.Template != null)
                {
                    foreach (Match match in actionRouteRegex.Matches(attrib.Template))
                    {
                        actionRoutes.Add(match.Groups[1].Value);
                    }
                }

                ParameterInfo fromBody = null;
                foreach (var item in methodInfo.GetParameters())
                {
                    if (item.GetCustomAttribute <FromBodyAttribute>() != null)
                    {
                        fromBody = item;
                    }
                    else if (!actionRoutes.Contains(item.Name))
                    {
                        writer.Write($"queryString.Add(nameof(@{item.Name}), ");
                        if (item.ParameterType != typeof(string))
                        {
                            writer.WriteLine($"System.Convert.ToString(@{item.Name}));");
                        }
                        else
                        {
                            writer.WriteLine($"@{item.Name});");
                        }
                    }
                }
                if (fromBody?.ParameterType == typeof(string))
                {
                    writer.WriteLine($"using (var request = this.CreateStringRequest({attrib.GetMethod()}, path, queryString, @{fromBody.Name})) {{");
                }
                else if (fromBody == null)
                {
                    writer.WriteLine($"using (var request = this.CreateRequest({attrib.GetMethod()}, path, queryString)) {{");
                }
                else
                {
                    writer.WriteLine($"using (var request = this.CreateJsonRequest<{new DotNetType(fromBody.ParameterType)}>({attrib.GetMethod()}, path, queryString, @{fromBody.Name})) {{");
                }
                writer.Tab();
                //skip "return" if return type is void or Task
                if (!method.ReturnType.IsVoid)
                {
                    writer.Write("return ");
                }

                writer.Write($"await this.Invoke");
                if (!method.ReturnType.IsVoid && method.ReturnType != new DotNetType(typeof(string)))
                {
                    writer.Write($"<{method.ReturnType.RemoveAsync()}>");
                }
                writer.WriteLine("(request);");
                writer.Write("}");
            }
            method.Body.Content = sb.ToString();
            method.Async        = true;
            return(method);
        }