コード例 #1
0
        public ClientActionHanler(MethodInfo method)
        {
            MethodInfo    = method;
            Method        = "GET";
            Name          = method.Name;
            DeclaringType = method.DeclaringType;
            MethodType    = MethodInfo.ReturnType;
            Async         = false;
            if (MethodInfo.ReturnType != typeof(void))
            {
                if (MethodInfo.ReturnType.Name == "Task`1" || MethodInfo.ReturnType.Name == "ValueTask`1" || MethodInfo.ReturnType == typeof(ValueTask))
                {
                    Async = true;
                    if (MethodInfo.ReturnType.IsGenericType)
                    {
                        ReturnType = MethodInfo.ReturnType.GetGenericArguments()[0];
                    }
                }
                else
                {
                    ReturnType = MethodInfo.ReturnType;
                }
            }
            foreach (CHeaderAttribute h in DeclaringType.GetCustomAttributes <CHeaderAttribute>())
            {
                if (!string.IsNullOrEmpty(h.Name) && !string.IsNullOrEmpty(h.Value))
                {
                    mHeaders[h.Name] = h.Value;
                }
            }

            foreach (CHeaderAttribute h in method.GetCustomAttributes <CHeaderAttribute>())
            {
                if (!string.IsNullOrEmpty(h.Name) && !string.IsNullOrEmpty(h.Value))
                {
                    mHeaders[h.Name] = h.Value;
                }
            }

            foreach (CQueryAttribute q in DeclaringType.GetCustomAttributes <CQueryAttribute>())
            {
                if (!string.IsNullOrEmpty(q.Name) && !string.IsNullOrEmpty(q.Value))
                {
                    mQueryString[q.Name] = q.Value;
                }
            }

            foreach (CQueryAttribute q in method.GetCustomAttributes <CQueryAttribute>())
            {
                if (!string.IsNullOrEmpty(q.Name) && !string.IsNullOrEmpty(q.Value))
                {
                    mQueryString[q.Name] = q.Value;
                }
            }

            Formater = method.GetCustomAttribute <FormaterAttribute>();
            if (Formater == null)
            {
                Formater = DeclaringType.GetCustomAttribute <FormaterAttribute>();
            }
            var get = method.GetCustomAttribute <GetAttribute>();

            if (get != null)
            {
                Method = Request.GET;
                if (!string.IsNullOrEmpty(get.Route))
                {
                    RouteTemplateMatch = new RouteTemplateMatch(get.Route);
                }
            }
            var post = method.GetCustomAttribute <PostAttribute>();

            if (post != null)
            {
                Method = Request.POST;
                if (!string.IsNullOrEmpty(post.Route))
                {
                    RouteTemplateMatch = new RouteTemplateMatch(post.Route);
                }
            }
            var del = method.GetCustomAttribute <DelAttribute>();

            if (del != null)
            {
                Method = Request.DELETE;
                if (!string.IsNullOrEmpty(del.Route))
                {
                    RouteTemplateMatch = new RouteTemplateMatch(del.Route);
                }
            }
            var put = method.GetCustomAttribute <PutAttribute>();

            if (put != null)
            {
                Method = Request.PUT;
                if (!string.IsNullOrEmpty(put.Route))
                {
                    RouteTemplateMatch = new RouteTemplateMatch(put.Route);
                }
            }
            Controller = this.DeclaringType.GetCustomAttribute <ControllerAttribute>();
            if (Controller != null)
            {
                if (!string.IsNullOrEmpty(Controller.BaseUrl))
                {
                    BaseUrl = Controller.BaseUrl;
                }
            }
            if (string.IsNullOrEmpty(BaseUrl))
            {
                BaseUrl = "/";
            }
            if (BaseUrl[0] != '/')
            {
                BaseUrl = "/" + BaseUrl;
            }
            if (BaseUrl.Substring(BaseUrl.Length - 1, 1) != "/")
            {
                BaseUrl += "/";
            }
            int index = 0;

            foreach (var p in method.GetParameters())
            {
                ClientActionParameter cap = new ClientActionParameter();
                cap.Name          = p.Name;
                cap.ParameterType = p.ParameterType;
                cap.Index         = index;
                index++;
                CHeaderAttribute cHeader = p.GetCustomAttribute <CHeaderAttribute>();
                if (cHeader != null)
                {
                    if (!string.IsNullOrEmpty(cHeader.Name))
                    {
                        cap.Name = cHeader.Name;
                    }
                    mHeaderParameters.Add(cap);
                }
                else
                {
                    CQueryAttribute cQuery = p.GetCustomAttribute <CQueryAttribute>();
                    if (cQuery != null)
                    {
                        if (!string.IsNullOrEmpty(cQuery.Name))
                        {
                            cap.Name = cQuery.Name;
                        }
                        mQueryStringParameters.Add(cap);
                    }
                    else
                    {
                        if (RouteTemplateMatch != null && RouteTemplateMatch.Items.Find(i => i.Name == p.Name) != null)
                        {
                            mRouteParameters.Add(cap);
                        }
                        else
                        {
                            mDataParameters.Add(cap);
                        }
                    }
                }
            }
        }