/// <summary> /// 获取详细方法模型 根据控制器名称和方法名称 /// </summary> /// <param name="ControllerName">控制器名称</param> /// <param name="MethodName">方法名称</param> /// <returns></returns> private MethodModel GetMethodModelByName(string ControllerName, string MethodName) { Assembly asmb = Assembly.LoadFrom(assemlyUrl); List <Member> propertyMembers = LoadXMLHelper.LoadXML().Where(t => t.PreWord == 'P' || t.PreWord == 'F').ToList(); Member member = LoadXMLHelper.LoadXML().Where(t => t.PreWord == 'M' && t.ControllerName == ControllerName && t.MethodName == MethodName).FirstOrDefault(); MethodModel methodModel = new MethodModel(); if (member == null) { return(methodModel); } methodModel = new MethodModel(member); Type type = asmb.GetType(member.ControllerNamespace); MethodInfo methodInfo = type.GetMethod(member.MethodName); ParameterInfo[] paramInfoArr = methodInfo.GetParameters(); foreach (ParameterInfo paramInfo in paramInfoArr) { JsonModle paramModel = new JsonModle(); paramModel.ColumName = paramInfo.Name; Param param = member.ParamList.Where(t => t.Name == paramInfo.Name).FirstOrDefault(); if (param != null) { paramModel.ColumSummary = param.Value; } else { paramModel.ColumSummary = string.Empty; } paramModel.ColumType = GetTypeName(paramInfo.ParameterType); paramModel.ChildColumList = GetChildColumList(paramInfo.ParameterType, propertyMembers, 1); paramModel.DefaultValue = paramInfo.DefaultValue.ToString(); methodModel.ParamList.Add(paramModel); } //响应内容 if (!string.IsNullOrWhiteSpace(member.Returns.AssemblyName) && !string.IsNullOrWhiteSpace(member.Returns.TypeFullName)) { try { string return_assemlyUrl = System.AppDomain.CurrentDomain.RelativeSearchPath + "/" + member.Returns.AssemblyName; Assembly return_asmb = Assembly.LoadFrom(return_assemlyUrl); Type return_type = return_asmb.GetType(member.Returns.TypeFullName); methodModel.ReturnList = GetChildColumList(return_type, propertyMembers, 0); } catch { } } return(methodModel); }
/// <summary> /// 获取控制器模型列表 /// </summary> /// <returns></returns> private List <ControllerModel> GetControllerModelList() { List <Member> memberList = LoadXMLHelper.LoadXML().Where(t => t.ControllerName != "" && (t.PreWord == 'M' || t.PreWord == 'T')).ToList(); List <IGrouping <string, Member> > groupList = memberList.GroupBy(t => t.ControllerName).ToList(); List <ControllerModel> controllerList = new List <ControllerModel>(); foreach (IGrouping <string, Member> groups in groupList) { ControllerModel c_model = new ControllerModel(); c_model.ControllerName = groups.FirstOrDefault().ControllerName; Member g_summary = memberList.Where(t => t.ControllerName == c_model.ControllerName && t.PreWord == 'T').FirstOrDefault(); if (g_summary != null) { c_model.ControllerSummary = g_summary.Summary.Value; } else { c_model.ControllerSummary = string.Empty; } controllerList.Add(c_model); } return(controllerList.OrderBy(t => t.ControllerName).ToList()); }
/// <summary> /// 获取方法模型列表 根据控制器名称 /// </summary> /// <param name="ControllerName">控制器名称</param> /// <returns></returns> private List <MethodModel> GetMethodModelList(string ControllerName) { Assembly asmb = Assembly.LoadFrom(assemlyUrl); List <Member> memberList = LoadXMLHelper.LoadXML().Where(t => t.PreWord == 'M' && t.ControllerName == ControllerName).ToList(); List <MethodModel> methodList = new List <MethodModel>(); foreach (Member member in memberList) { MethodModel m_model = new MethodModel(member); //反射 Type type = asmb.GetType(member.ControllerNamespace); MethodInfo methodInfo = null; try { methodInfo = type.GetMethod(member.MethodName); } catch { } if (methodInfo == null) { continue; } methodInfo.CustomAttributes.ToList().ForEach(ca => { switch (ca.AttributeType.Name) { case "HttpGetAttribute": m_model.MethodType = "Get"; break; case "HttpPostAttribute": m_model.MethodType = "Post"; break; case "HttpHeadAttribute": m_model.MethodType = "Head"; break; case "HttpOptionsAttribute": m_model.MethodType = "Options"; break; case "HttpPutAttribute": m_model.MethodType = "Put"; break; case "HttpDeleteAttribute": m_model.MethodType = "Delete"; break; case "HttpPatchAttribute": m_model.MethodType = "Patch"; break; default: if (string.IsNullOrEmpty(m_model.MethodType)) { m_model.MethodType = "Get"; } break; } }); methodList.Add(m_model); } return(methodList); }