コード例 #1
0
        /// <summary>
        ///     注册方法
        /// </summary>
        /// <param name="name">方法外部方法名称,如 v1\auto\getdid </param>
        /// <param name="action">动作</param>
        /// <param name="info">反射信息</param>
        public void RegistAction(string name, ApiAction action, ApiActionInfo info = null)
        {
            if (!_apiActions.ContainsKey(name))
            {
                action.Name = name;
                _apiActions.Add(name, action);
            }
            else
            {
                _apiActions[name] = action;
            }

            ZeroTrace.SystemLog(StationName,
                                info != null
                    ? $"{name}({info.Controller}.{info.Name}) is registed."
                    : $"{name} is registed");
        }
コード例 #2
0
 /// <summary>
 ///     注册方法
 /// </summary>
 /// <param name="name">方法外部方法名称,如 v1\auto\getdid </param>
 /// <param name="action">动作</param>
 /// <param name="info">反射信息</param>
 public void RegistAction(string name, ApiAction action, ApiActionInfo info = null)
 {
     if (!_apiActions.ContainsKey(name))
     {
         action.Name = name;
         _apiActions.Add(name, action);
     }
     else
     {
         _apiActions[name] = action;
     }
     if (info != null)
     {
         ZeroTrace.WriteInfo(StationName, $"{name}({info?.Controller}.{info.Name}) is registed.");
     }
     else
     {
         ZeroTrace.WriteInfo(StationName, $"{name} is registed");
     }
 }
コード例 #3
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);
            }
        }
コード例 #4
0
        /// <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);
            }
        }
コード例 #5
0
        /// <summary>
        ///     注册方法
        /// </summary>
        /// <param name="name">方法外部方法名称,如 v1\auto\getdid </param>
        /// <param name="action">动作</param>
        /// <param name="info">反射信息</param>
        /// <param name="access">访问设置</param>
        public ApiAction RegistAction(string name, Func <IApiResult> action, ApiAccessOption access, ApiActionInfo info = null)
        {
            var a = new ApiAction <IApiResult>
            {
                Name   = name,
                Action = action,
                Access = access
            };

            RegistAction(name, a, info);
            return(a);
        }
コード例 #6
0
        /// <summary>
        ///     注册方法
        /// </summary>
        /// <param name="name">方法外部方法名称,如 v1\auto\getdid </param>
        /// <param name="action">动作</param>
        /// <param name="access">访问设置</param>
        /// <param name="info">反射信息</param>
        public ApiAction RegistAction <TArgument, TResult>(string name, Func <TArgument, TResult> action, ApiAccessOption access, ApiActionInfo info = null)
            where TArgument : class, IApiArgument
            where TResult : ApiResult
        {
            var a = new ApiAction <TArgument, TResult>
            {
                Name   = name,
                Action = action,
                Access = access
            };

            RegistAction(name, a, info);

            return(a);
        }