/// <summary> /// 查找API /// </summary> public void FindApies() { XmlMember.Load(Assembly); StationInfo.Add(StationName, _defStation = new StationDocument { Name = StationName }); var types = Assembly.GetTypes().Where(p => p.IsSubclassOf(typeof(ApiController))).ToArray(); foreach (var type in types) { FindApi(type, false); } RegistToZero(); RegistDocument(); }
/// <summary> /// 查找API /// </summary> /// <param name="type"></param> /// <param name="onlyDoc"></param> private void FindApi(Type type, bool onlyDoc) { if (type.IsAbstract) { return; } StationDocument station; var sa = type.GetCustomAttribute <StationAttribute>(); if (sa != null) { if (!StationInfo.TryGetValue(sa.Name, out station)) { StationInfo.Add(sa.Name, station = new StationDocument { Name = sa.Name }); } } else { station = _defStation; } //station.Copy(XmlMember.Find(type)); string routeHead = null; var attrib = type.GetCustomAttribute <RouteAttribute>(); if (attrib != null) { routeHead = attrib.Name; } else { var attrib2 = type.GetCustomAttribute <RoutePrefixAttribute>(); if (attrib2 != null) { routeHead = attrib2.Name; } } if (string.IsNullOrWhiteSpace(routeHead)) { routeHead = null; } else { routeHead = routeHead.Trim(' ', '\t', '\r', '\n', '/') + "/"; } var methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); var xdoc = XmlMember.Find(type); foreach (var method in methods) { var route = method.GetCustomAttribute <RouteAttribute>(); if (route == null) { //ZeroTrace.WriteError("ApiDiscover", "exclude", station.Name, type.Name, method.Name); continue; } if (method.Name.Length > 4 && (method.Name.IndexOf("get_") == 0 || method.Name.IndexOf("set_") == 0)) { continue; } if (method.GetParameters().Length > 1) { //ZeroTrace.WriteError("ApiDiscover", "argument size must 0 or 1", station.Name, type.Name, method.Name); continue; } var name = route?.Name == null ? $"{routeHead}{method.Name}" : $"{routeHead}{route.Name.Trim(' ', '\t', '\r', '\n', '/')}"; var accessOption = method.GetCustomAttribute <ApiAccessOptionFilterAttribute>(); var ca = method.GetAttribute <CategoryAttribute>(); var api = new ApiActionInfo { Name = method.Name, ApiName = route?.Name ?? method.Name, RouteName = name, Category = ca?.Category ?? xdoc?.Caption, AccessOption = accessOption?.Option ?? ApiAccessOption.Public | ApiAccessOption.ArgumentCanNil, ResultInfo = ReadEntity(method.ReturnType, "result") }; var doc = XmlMember.Find(type, method.Name, "M"); api.Copy(doc); var arg = method.GetParameters().FirstOrDefault(); api.HaseArgument = arg != null; //动态生成并编译 if (api.HaseArgument) { api.ArgumentInfo = ReadEntity(arg.ParameterType, "argument") ?? new TypeDocument(); api.ArgumentInfo.Name = arg.Name; if (doc != null) { api.ArgumentInfo.Caption = doc.Arguments.Values.FirstOrDefault(); } if (!onlyDoc) { api.ArgumenType = arg.ParameterType; api.ArgumentAction = CreateFunc <IApiArgument, IApiResult>(type.GetTypeInfo(), method.Name, arg.ParameterType.GetTypeInfo(), method.ReturnType.GetTypeInfo()); } } else if (!onlyDoc) { api.Action = CreateFunc <IApiResult>(type.GetTypeInfo(), method.Name, method.ReturnType.GetTypeInfo()); } station.Aips.Add(api.RouteName, api); } }