Ejemplo n.º 1
0
        /// <summary>
        /// 查找API
        /// </summary>
        /// <param name="type"></param>
        private void FindApi(Type type)
        {
            var methods = type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance
                                          | BindingFlags.Public
                                          | BindingFlags.NonPublic);

            foreach (var method in methods)
            {
                var route = method.GetCustomAttribute <RouteAttribute>();
                if (route == null)
                {
                    continue;
                }
                var info = new ApiActionInfo
                {
                    FunctionName = method.Name,
                    RouteName    = route.Name
                };
                var pars = method.GetParameters();
                if (method.GetParameters().Length == 0)
                {
                    info.HaseArgument = false;
                    info.Action       = TypeExtend.CreateFunc <IApiResult>(type.GetTypeInfo(), method.Name, method.ReturnType.GetTypeInfo());
                }
                else if (method.GetParameters().Length == 1)
                {
                    info.HaseArgument   = true;
                    info.ArgumentAction = TypeExtend.CreateFunc <IApiArgument, IApiResult>(type.GetTypeInfo(), method.Name, pars[0].ParameterType.GetTypeInfo(), method.ReturnType.GetTypeInfo());
                }
                else
                {
                    continue;
                }
                var accessOption = method.GetCustomAttribute <ApiAccessOptionFilterAttribute>();
                if (accessOption != null)
                {
                    info.AccessOption = accessOption.Option;
                }
                ApiItems.Add(info.RouteName, info);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 查找API
        /// </summary>
        /// <param name="type"></param>
        /// <param name="onlyDoc"></param>
        private void FindApi(Type type, bool onlyDoc)
        {
            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;
            }
            var xdoc = XmlMember.Find(type);
            //station.Copy(XmlMember.Find(type));
            string routeHead = null;
            var    attrib    = type.GetCustomAttribute <RouteAttribute>();

            if (attrib != null)
            {
                routeHead = attrib.Name;
            }

            if (string.IsNullOrWhiteSpace(routeHead))
            {
                routeHead = null;
            }
            else
            {
                routeHead = routeHead.Trim(' ', '\t', '\r', '\n', '/') + "/";
            }

            var methods = type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance
                                          | BindingFlags.Public
                                          | BindingFlags.NonPublic);

            foreach (var method in methods)
            {
                if (method.GetParameters().Length > 1)
                {
                    ZeroTrace.WriteError("ApiDiscover", "argument size must 0 or 1", station.Name, type.Name, method.Name);
                    continue;
                }
                var route = method.GetCustomAttribute <RouteAttribute>();
                if (route == null && !method.IsPublic)
                {
                    ZeroTrace.WriteError("ApiDiscover", "exclude", 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,
                    RouteName    = name,
                    Category     = ca?.Category ?? xdoc.Caption,
                    Controller   = type.FullName,
                    AccessOption = accessOption != null ? accessOption.Option : ApiAccessOption.Public | ApiAccessOption.Anymouse | ApiAccessOption.ArgumentCanNil,
                    ResultInfo   = ReadEntity(method.ReturnType, "result")
                };
                station.Aips.Add(api.RouteName, api);
                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 = TypeExtend.CreateFunc <IApiArgument, IApiResult>(type.GetTypeInfo(),
                                                                                              method.Name,
                                                                                              arg.ParameterType.GetTypeInfo(),
                                                                                              method.ReturnType.GetTypeInfo());
                    }
                }
                else if (!onlyDoc)
                {
                    api.Action = TypeExtend.CreateFunc <IApiResult>(type.GetTypeInfo(), method.Name, method.ReturnType.GetTypeInfo());
                }
            }
        }