我们需要获取类,属性,方法的描述。这个跟获取枚举的描述一样,需要我们通过反射来做。这还需要我们的利用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 = "";
} /// <summary>
/// 得到方法重载
/// </summary>
[Description("方法描述3")]
public void Eat(string aa)
{
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 > ? attributes[].Description : ""; } /// <summary>
/// 根据方法名获取描述
/// </summary>
/// <param name="method">方法名</param>
/// <param name="t">类型</param>
/// <param name="types">参数类型</param>
/// <returns></returns>
public static string GetDescriptionByMethod(this string method, Type t, params Type[] types)
{ System.Reflection.MethodInfo fi = t.GetMethod(method, types);
if (fi != null)
{
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);
return attributes.Length > ? attributes[].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 > ? attributes[].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);
string meDes2 = "Eat".GetDescriptionByMethod(t, new Type[] { typeof(string) });
Console.WriteLine($"类:TestDes:{classDes}");
Console.WriteLine($"属性:Name:{proDes}");
Console.WriteLine($"方法:Eat:{meDes}");
Console.WriteLine($"方法重载:Eat:{meDes2}");
Console.ReadLine();

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 = -,
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);
}
}

System.Reflection 获取描述的更多相关文章

  1. System.Reflection.Assembly.GetEntryAssembly()获取的为当前已加载的程序集

    今天在使用System.Reflection.Assembly.GetEntryAssembly()获取程序集时,发现获取的程序集不全.原来是因为C#的程序集为延迟加载,此方法只获取当前已加载的,未加 ...

  2. 【C#基础】System.Reflection (反射)

    在使用.NET创建的程序或组件时,元数据(metadata)和代码(code)都存储于"自成一体"的单元中,这个单元称为装配件.我们可以在程序运行期间访问这些信息.在System. ...

  3. 基础命名空间:反射 using System.Reflection

    反射概念:       .Net的应用程序由几个部分:‘程序集(Assembly)’.‘模块(Module)’.‘类型(class)’组成,程序集包含模块 模块包含类型,类型又包含 成员,而反射提供一 ...

  4. System.Reflection.Emit学习

    C#反射发出System.Reflection.Emit学习 分享: 1 一.System.Reflection.Emit概述 Emit,可以称为发出或者产生.与Emit相关的类基本都存在于Syste ...

  5. System.Reflection.Emit摘记

    动态类型在.net中都是用什么类型来表示的.程序集:System.Reflection.Emit.AssemblyBuilder(定义并表示动态程序集)构造函数:System.Reflection.E ...

  6. 利用system.reflection遍历一个类的变量成员

    假设有下面一个类,在程序中已初始化,如何获取里面的变量成员name,age,onduty及其值呢? public class Employee { public string name; public ...

  7. C#反射发出System.Reflection.Emit学习

    一.System.Reflection.Emit概述 Emit,可以称为发出或者产生.与Emit相关的类基本都存在于System.Reflection.Emit命名空间下.反射,我们可以取得形如程序集 ...

  8. C# System.Reflection.Assembly动态加载资源文件

    需求:需要做甘特图的显示,并且在甘特中加载图片.图片太多,写判断代码太多.用反射吧. 核心代码: try { if (stateColour < 0) return null; System.R ...

  9. 反射基础 System.Reflection

    一.获取程序集Assembly 1.获取当前运行的程序集 System.Reflection.Assembly[] asm = AppDomain.CurrentDomain.GetAssemblie ...

随机推荐

  1. 第二天-while循环 格式化输出 运算符 编码

    一.while循环 while 条件: 语句块(循环体)     #判断条件是否成立,若成立执行循环体,然后再次判断条件...直到不满足跳出循环 else: 当条件不成立的时候执行这里,和break没 ...

  2. LOJ6066:「2017 山东一轮集训 Day3」第二题

    传送门 二分答案 \(k\),考虑如何 \(hash\) 使得做起来方便 把每个点挂在 \(k+1\) 级祖先上,考虑在祖先上删除 这道题巧妙在于其可以对于 \(dfs\) 序/括号序列 \(hash ...

  3. BZOJ1968 [Ahoi2005] 约数研究

    Description Input 只有一行一个整数 N(0 < N < 1000000). Output 只有一行输出,为整数M,即f(1)到f(N)的累加和. Sample Input ...

  4. Bootstrap 3.0的扁平化来了

    Bootstrap 3 RC1 发布了,从官方上看,Bootstrap 3 似乎也开始趋于扁平化的风格设计. 网站UI和Button bootstrap 2.3.2以下的整体UI和图标是以box-sh ...

  5. MySQL数据库(9)----使用连接实现多表检索

    有许多演示如何使用MySQL所支持的连接操作的示例,都用到了下列两个表 t1 和 t2: mysql> SELECT * FROM t1; +----+------+ | i1 | c1 | + ...

  6. Oracle客户端安装以及PL/SQL Developer安装方法

    1,安装Oracle客户端 2,配置数据库,如下: 安装路径:D:\app\ThinkPad\product\11.2.0\client_1\network\admin 建立文件:tnsnames.o ...

  7. Flutter知识点:数据存储之sqflite

    sqflite是一款轻量级的关系型数据库,类似SQLite. 在Flutter平台我们使用sqflite库来同时支持Android 和iOS. 使用介绍 1.首选需要在pubspec.yaml 导入库 ...

  8. HTTP状态码和HTTP请求头

    HTTP报文是在Web服务器和浏览器之间进行交换的文本数据,一种是从浏览器发出的请求,一种是服务器发出的响应. 请求报文的第一行包括:1.请求方法 2.当前使用的HTTP协议版本 3.请求地址 GET ...

  9. 理解JMeter聚合报告(Aggregate Report)

    Aggregate Report 是 JMeter 常用的一个 Listener,中文被翻译为“聚合报告”.今天再次有同行问到这个报告中的各项数据表示什么意思,顺便在这里公布一下,以备大家查阅. 如果 ...

  10. 个人总结4-dbutils总结

    昨天学习了dbutils的使用方法,简化了使用的步骤,可以使用三四步就可以写出来,queryRunner的使用方法有了简单的了解,目前可以使用dbutils实现最简单的增删改查. 今天准备学习准备写登 ...