/// <summary> /// 构建next执行链接 /// </summary> /// <param name="t"></param> /// <param name="parentKey"></param> /// <param name="entryRIE"></param> /// <param name="pointTypeList"></param> private void BuildNextRouteInvokeLink(Type t, string parentKey, List <RouteInvokeEntity> entryRIE, List <Type> pointTypeList) { var methods = t.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(p => _method_verbs_.Contains(p.Name.ToLower()) && (p.DeclaringType == t || p.DeclaringType == LType) && p.GetCustomAttribute(typeof(EWRARouteAttribute)) == null); var subtypes = pointTypeList.Where(p => p.GetTypeInfo().BaseType == t).ToList(); RouteInvokeEntity parentRIE = entryRIE[entryRIE.Count - 1]; var instance = (RestLogic)Activator.CreateInstance(t, true); foreach (var item in methods) { //parent_开头的参数只能有一个 if (item.GetParameters().Where(p => p.Name.StartsWith("parent_")).Count() != 1) { continue; } if (parentRIE.InvokeName != "get") { continue; } var parent_p = item.GetParameters().Where(p => p.Name.StartsWith("parent_")).First(); if (parentRIE.InvokeMethod.ReturnType != parent_p.ParameterType) { continue; } //参数类型只能为string if (item.GetParameters().Where(p => p.ParameterType != typeof(string) && !p.Name.StartsWith("parent_")).Count() > 0) { continue; } //判断该method是否对外开放,无EWRAIsOpenAttribute属性或IsOpen==false的才开放 var isopenattr = item.GetCustomAttribute <EWRAIsOpenAttribute>(false); if (isopenattr != null && !isopenattr.IsOpen) { continue; } var list = new List <RouteInvokeEntity>(); list.AddRange(entryRIE); var entity = new RouteInvokeEntity(); entity.InstanceType = t; entity.InvokeMethod = item; entity.InvokeName = item.Name.ToLower(); entity.Route = $"/{instance.Name}"; entity.ParameterCountWithOutParent = 0; entity.HasParentParameter = true; foreach (var p in entity.InvokeMethod.GetParameters().Where(p => !p.Name.StartsWith("parent_"))) { entity.Route += $"/{{{p.Name}}}"; entity.ParameterCountWithOutParent++; } var attrdesc = entity.InvokeMethod.GetCustomAttribute <EWRARouteDescAttribute>(); entity.RouteDesc = attrdesc == null ? "" : attrdesc.Desc; entity.RouteRegExpress = _reg_brace_p_.Replace(entity.Route, _parameter_reg_express); //是否可见 var attrvisible = entity.InvokeMethod.GetCustomAttribute <EWRAVisibleAttribute>(); if (attrvisible != null) { entity.IsVisible = attrvisible.IsVisible; } else { entity.IsVisible = true; } //添加参数描述 var attrinputs = entity.InvokeMethod.GetCustomAttributes <EWRAAddInputAttribute>(); if (attrinputs != null) { foreach (var attr in attrinputs) { entity.AddInputItem(attr); } } //添加返回数据的描述 var attroutput = entity.InvokeMethod.GetCustomAttribute <EWRAOutputDescAttribute>(); if (attroutput != null) { entity.OutputDesc.Desc = attroutput.Desc; entity.OutputDesc.FormatDesc = attroutput.FormatDesc; entity.OutputDesc.ReturnType = attroutput.ReturnType; } list.Add(entity); var key = $"{entity.InvokeName}:{parentKey.Split(':')[1]}{entity.Route}"; _d_invoke_.Add(key, list); //搜索子类 foreach (var subt in subtypes) { BuildNextRouteInvokeLink(subt, key, list, pointTypeList); } } }
/// <summary> /// 基于RouteAttribute描述构建自定义独立API入口 /// </summary> /// <param name="t"></param> /// <param name="pointTypeList"></param> private void BuildSingleEntryInvokeLink(Type entryType, List <Type> pointTypeList) { var types = pointTypeList.Where(p => p == entryType || p.GetTypeInfo().IsSubclassOf(entryType)); foreach (Type item in types) { var methods = item.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(p => p.GetCustomAttribute <EWRARouteAttribute>() != null); foreach (var m in methods) { //参数类型只能为string if (m.GetParameters().Where(p => p.ParameterType != typeof(string)).Count() > 0) { continue; } //声明的类不是item的则不处理 if (m.DeclaringType != item) { continue; } var attri = m.GetCustomAttribute <EWRARouteAttribute>(); if (attri.MethodVerb == "" || !_method_verbs_.Contains(attri.MethodVerb)) { continue; } //判断该method是否对外开放,无EWRAIsOpenAttribute属性或IsOpen==false的才开放 var isopenattr = m.GetCustomAttribute <EWRAIsOpenAttribute>(false); if (isopenattr != null && !isopenattr.IsOpen) { continue; } var list = new List <RouteInvokeEntity>(); var entity = new RouteInvokeEntity(); entity.InstanceType = item; entity.InvokeMethod = m; entity.InvokeName = attri.MethodVerb; entity.Route = $"/{attri.Route}"; entity.ParameterCountWithOutParent = 0; entity.HasParentParameter = false; foreach (var p in entity.InvokeMethod.GetParameters()) { //如果route中已经含有对应的参数定义,则不自动添加 if (!attri.Route.Contains($"/{{{p.Name}}}")) { entity.Route += $"/{{{p.Name}}}"; } entity.ParameterCountWithOutParent++; } var attrdesc = entity.InvokeMethod.GetCustomAttribute <EWRARouteDescAttribute>(); entity.RouteDesc = attrdesc == null ? "" : attrdesc.Desc; entity.RouteRegExpress = _reg_brace_p_.Replace(entity.Route, _parameter_reg_express); //是否可见 var attrvisible = entity.InvokeMethod.GetCustomAttribute <EWRAVisibleAttribute>(); if (attrvisible != null) { entity.IsVisible = attrvisible.IsVisible; } else { entity.IsVisible = true; } //添加参数描述 var attrinputs = entity.InvokeMethod.GetCustomAttributes <EWRAAddInputAttribute>(); if (attrinputs != null) { foreach (var attr in attrinputs) { entity.AddInputItem(attr); } } //添加返回数据的描述 var attroutput = entity.InvokeMethod.GetCustomAttribute <EWRAOutputDescAttribute>(); if (attroutput != null) { entity.OutputDesc.Desc = attroutput.Desc; entity.OutputDesc.FormatDesc = attroutput.FormatDesc; entity.OutputDesc.ReturnType = attroutput.ReturnType; } list.Add(entity); var key = $"{attri.MethodVerb}:{attri.Route}"; if (!_d_invoke_.ContainsKey(key)) { _d_invoke_.Add(key, list); } else { Console.WriteLine($"\n路由路径:{key}已经存在;相关信息:类名-{item.Name},方法名称-{m.Name},参数-{entity.Route.Replace($"/{entity.Route}", "").Replace("/", ",")}"); continue; } } } }
/// <summary> /// 构建入口路由 /// </summary> /// <param name="t"></param> /// <param name="pointTypeList"></param> private void BuildEntryRouteInvokeLink(Type t, List <Type> pointTypeList) { var methods = t.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(p => _method_verbs_.Contains(p.Name.ToLower()) && (p.DeclaringType == t || p.DeclaringType == LType) && p.GetCustomAttribute(typeof(EWRARouteAttribute)) == null); var subtypes = pointTypeList.Where(p => p.GetTypeInfo().BaseType == t).ToList(); var instance = (RestLogic)Activator.CreateInstance(t, true); var tmpdic = new Dictionary <string, List <RouteInvokeEntity> >(); foreach (var item in methods) { //入口logic不可拥有名称开头为parent_的参数 if (item.GetParameters().Where(p => p.Name.StartsWith("parent_")).Count() > 0) { continue; } //参数类型只能为string if (item.GetParameters().Where(p => p.ParameterType != typeof(string)).Count() > 0) { continue; } //判断该method是否对外开放,无EWRAIsOpenAttribute属性或IsOpen==false的才开放 var isopenattr = item.GetCustomAttribute <EWRAIsOpenAttribute>(false); if (isopenattr != null && !isopenattr.IsOpen) { continue; } var entity = new RouteInvokeEntity(); entity.InstanceType = t; entity.InvokeMethod = item; entity.InvokeName = item.Name.ToLower(); entity.Route = $"/{instance.Name}"; entity.ParameterCountWithOutParent = 0; entity.HasParentParameter = false; foreach (var p in entity.InvokeMethod.GetParameters()) { entity.Route += $"/{{{p.Name}}}"; entity.ParameterCountWithOutParent++; } var attrdesc = entity.InvokeMethod.GetCustomAttribute <EWRARouteDescAttribute>(); entity.RouteDesc = attrdesc == null ? "" : attrdesc.Desc; entity.RouteRegExpress = _reg_brace_p_.Replace(entity.Route, _parameter_reg_express); //是否可见 var attrvisible = entity.InvokeMethod.GetCustomAttribute <EWRAVisibleAttribute>(); if (attrvisible != null) { entity.IsVisible = attrvisible.IsVisible; } else { entity.IsVisible = true; } //添加参数描述 var attrinputs = entity.InvokeMethod.GetCustomAttributes <EWRAAddInputAttribute>(); if (attrinputs != null) { foreach (var attr in attrinputs) { entity.AddInputItem(attr); } } //添加返回数据的描述 var attroutput = entity.InvokeMethod.GetCustomAttribute <EWRAOutputDescAttribute>(); if (attroutput != null) { entity.OutputDesc.Desc = attroutput.Desc; entity.OutputDesc.FormatDesc = attroutput.FormatDesc; entity.OutputDesc.ReturnType = attroutput.ReturnType; } var key = $"{entity.InvokeName}:{entity.Route}"; if (_d_invoke_.ContainsKey(key)) { if (entity.InvokeMethod.DeclaringType == t && _d_invoke_[key][0].InvokeMethod.DeclaringType != t) { _d_invoke_[key][0] = entity; tmpdic[key][0] = entity; } } else { var list = new List <RouteInvokeEntity>(); list.Add(entity); _d_invoke_.Add(key, list); tmpdic.Add(key, list); } } foreach (var item in tmpdic) { //搜索子类 foreach (var subt in subtypes) { BuildNextRouteInvokeLink(subt, item.Key, item.Value, pointTypeList); } } tmpdic.Clear(); //子类独立入口路径建立 foreach (var subt in subtypes) { BuildEntryRouteInvokeLink(subt, pointTypeList); } }