public static JObject GetControllerInfo(params Type[] controllerTypes) { var res = new JObject(); foreach (var type in controllerTypes) { var routePrefix = type.GetCustomAttributes <RoutePrefixAttribute>().First().Prefix; var controller = new JObject(); foreach (var methodInfo in type.GetMethods()) { var verbs = GetMethods(methodInfo); if (verbs.Length > 0) { var method = new JObject { { "Method", new JValue(verbs[0]) }, { "Route", new JValue($"{routePrefix}/{methodInfo.GetCustomAttributes<RouteAttribute>().First().Template}") } }; var body = new JObject(); var parameters = new JObject(); foreach (var parameterInfo in methodInfo.GetParameters()) { if (parameterInfo.GetCustomAttributes <FromBodyAttribute>().FirstOrDefault() == null) { parameters.AddParameterInfo(parameterInfo); } else { body.AddParameterInfo(parameterInfo); } } if (parameters.Count > 0) { method.Add("Params", parameters); } if (body.Count > 0) { method.Add("Body", body); } var resAttr = methodInfo.GetCustomAttributes <ResponseTypeAttribute>().FirstOrDefault(); if (resAttr != null) { var response = new JObject(); response.AddProperties(resAttr.ResponseType); if (response.Count > 0) { method.Add("Response", response); } } controller.Add(methodInfo.Name, method); } } res.Add(Regex.Replace(type.Name, "Controller$", ""), controller); } return(res); }
public static void AddParameterInfo(this JObject obj, ParameterInfo parameterInfo) { if (parameterInfo.ParameterType.IsPrimitive || parameterInfo.ParameterType == typeof(string) || parameterInfo.ParameterType == typeof(Guid)) { obj.Add(parameterInfo.Name, parameterInfo.ParameterType.Name); } else if (parameterInfo.ParameterType.IsNullable()) { obj.Add(parameterInfo.Name, parameterInfo.ParameterType.GenericTypeArguments[0].Name); } else { obj.AddProperties(parameterInfo.ParameterType); } }