【笔记】使用接口的方式,将有[RequestControl(RequestorType.Admin)]的接口查询出来

   [Route("adm/getInfo")]
    [HttpGet]
    public string GetInfo()
    {
        var types = typeof(GCP.Server.WebAPI.Controllers.HomeController).Assembly.GetTypes().Where(m => m.Name.EndsWith("Controller"));
        System.Text.StringBuilder sb = new System.Text.StringBuilder(100);
        sb.Append("接口").Append(",").Append("类型").Append(",").Append("注释").AppendLine();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(@"G:\GCP\SourceCode\GCP\GCP.WebAPI\App_Data\GCP.WebAPI.XML"))
        {
            System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
            xml.Load(@"G:\GCP\SourceCode\GCP\GCP.WebAPI\App_Data\GCP.WebAPI.XML");
            var members = xml.LastChild.LastChild.ChildNodes;
            foreach (var t in types)
            {
                var routePrefix = t.GetCustomAttributes(false).FirstOrDefault(m => m.ToString().Contains("RoutePrefix")) as RoutePrefixAttribute;
                if (routePrefix == null)
                {
                    continue;
                }
                string prefix = routePrefix.Prefix;
                var ms = t.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
                    .Where(m => m.GetCustomAttributes(false).Any(n => n.GetType() == typeof(RequestControlAttribute)));
                foreach (var m in ms)
                {
                    var requestCtrl = m.GetCustomAttributes(typeof(RequestControlAttribute), false)[0] as RequestControlAttribute;
                    var routeAttr = m.GetCustomAttributes(typeof(RouteAttribute), false)[0] as RouteAttribute;
                    Row r = new Row
                    {
                        Url = prefix + "/" + routeAttr.Template,
                        Type = requestCtrl.requestorType.ToString(),
                        Content = GetContent(members, m)
                    };
                    sb.Append(r.Url).Append(",").Append(r.Type).Append(",").Append(r.Content).AppendLine();
                }
            }
        }
        System.IO.File.WriteAllText("d:/info.csv", sb.ToString(), System.Text.Encoding.UTF8);
        return "OK";
    }
    private string GetContent(XmlNodeList members, MethodInfo m)
    {
        for (int i = 0; i < members.Count; i++)
        {
            var mem = members[i];
            if (mem.Attributes["name"].Value.Contains(m.DeclaringType.ToString() + "." + m.Name))
            {
                return mem.FirstChild.InnerText.Replace("\r\n", "").Replace(",", "_").Trim();
            }
        }
        return "";
    }
    class Row
    {
        public string Url { get; set; }
        public string Type { get; set; }
        public string Content { get; set; }
    }

你可能感兴趣的:(【笔记】使用接口的方式,将有[RequestControl(RequestorType.Admin)]的接口查询出来)