ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

System.Reflection 获取描述

2019-03-23 16:44:04  阅读:277  来源: 互联网

标签:string DescriptionAttribute System actionExecutedContext 获取 attributes Descripti


我们需要获取类,属性,方法的描述。这个跟获取枚举的描述一样,需要我们通过反射来做。这还需要我们的利用System.ComponentModel:Description  的属性来完成。

新建一个类:使用的是:  System.ComponentModel:Description

 [Description("类的描述")]
    public class TestDes
    {
        [Description("id值")]
        public int Id { get; set; }


        [Description("名称")]
        public string Name { get; set; }

        /// <summary>
        /// 方法描述
        /// </summary>
        [Description("方法描述2")]
        public void Eat()
        {
            string d = "";
        }
    }

 

三个扩展方法:

  public static class Exl
    {
        /// <summary>
        /// 获取类的描述
        /// </summary>
        /// <param name="t">类型</param>
        /// <returns></returns>
        public static string GetDescription(this Type t)
        {
            DescriptionAttribute[] attributes =
                   (DescriptionAttribute[])t.GetCustomAttributes(
                       typeof(DescriptionAttribute), false);
            return attributes.Length > 0 ? attributes[0].Description : "";

        }

        /// <summary>
        /// 根据方法明描述
        /// </summary>
        /// <param name="method">方法明</param>
        /// <param name="t">类型</param>
        /// <returns></returns>
        public static string GetDescriptionByMethod(this string method, Type t)
        {
            System.Reflection.MethodInfo fi = t.GetMethod(method);
            if (fi != null)
            {
                DescriptionAttribute[] attributes =
                    (DescriptionAttribute[])fi.GetCustomAttributes(
                        typeof(DescriptionAttribute), false);
                return attributes.Length > 0 ? attributes[0].Description : "";
            }
            return "";
        }

        /// <summary>
        /// 根据方法明描述
        /// </summary>
        /// <param name="method">方法明</param>
        /// <param name="t">类型</param>
        /// <returns></returns>
        public static string GetDescriptionByProperty(this string property, Type t)
        {
            System.Reflection.PropertyInfo fi = t.GetProperty(property);
            if (fi != null)
            {
                DescriptionAttribute[] attributes =
                    (DescriptionAttribute[])fi.GetCustomAttributes(
                        typeof(DescriptionAttribute), false);
                return attributes.Length > 0 ? attributes[0].Description : "";
            }
            return "";
        }
    }

 

控制台:

 //获取类 需要命名空间+类名
            Type t = Type.GetType("ReflectionDemo.TestDes");
            //Attribute[] dd = (Attribute[])t.GetCustomAttributes(typeof(Attribute), false);
            string classDes = t.GetDescription();
            string proDes = "Name".GetDescriptionByProperty(t);
            string meDes = "Eat".GetDescriptionByMethod(t);
            Console.WriteLine($"类:TestDes:{classDes}");
            Console.WriteLine($"属性:Name:{proDes}");
            Console.WriteLine($"方法:Eat:{meDes}");

            Console.ReadKey();

 

 

webapi中的异常过滤器:

 public class MyErrorFilter : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            HttpActionContext context = actionExecutedContext.ActionContext;
            Type t = context.ControllerContext.Controller.GetType(); //得到控制器的类型
            string controllerDes = t.GetDescription(); //控制器的描述
            string controllerName = context.ActionDescriptor.ControllerDescriptor.ControllerName;//控制器的名称
            string actionName = context.ActionDescriptor.ActionName;//方法名
            string actionDes = actionName.GetDescriptionByMethod(t);//方法描述


            object obj = new
            {
                errcode = -1,
                errmsg = actionExecutedContext.Exception
            };
            actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.NotImplemented)
            {
                Content = new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json")
            };
            ////2.返回调用方具体的异常信息
            //if (actionExecutedContext.Exception is NotImplementedException)
            //{
            //    actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.NotImplemented);
            //}
            //else if (actionExecutedContext.Exception is TimeoutException)
            //{
            //    actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.RequestTimeout);
            //}
            ////.....这里可以根据项目需要返回到客户端特定的状态码。如果找不到相应的异常,统一返回服务端错误500
            //else
            //{
            //    actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError);

            //}
            base.OnException(actionExecutedContext);
        }
    }

 

标签:string,DescriptionAttribute,System,actionExecutedContext,获取,attributes,Descripti
来源: https://www.cnblogs.com/Sea1ee/p/10584474.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有