我们需要获取类,属性,方法的描述。这个跟获取枚举的描述一样,需要我们通过反射来做。这还需要我们的利用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. sql 模糊搜素拼接

    if($irb_order!=''){ $condition .= " AND d.irb_order like '%".$irb_order."%'"; } ...

  2. HDU P2089

    题目大意为,统计区间内不含4和62的数字的个数: 老实说,看到这题我是抵触的..... 基本上是数位DP的板子,话说数位DP好像全是模板题吧: 预处理,有关的整区间的数字个数: 调用已有的区间,求解0 ...

  3. vue项目性能优化(路由懒加载、gzip加速、cdn加速)

    前端工程性能优化一说意义深远悠长,本章主要介绍除了一些基础优化外如何实行路由懒加载.Gzip加速.CDN加速,让网页飞的快一些. 基础优化 老生常谈的一些: 不要在模板中写复杂的表达式 慎用watch ...

  4. Spring Data MongoDB 级联操作

    DBRef 方式关联 DBRef 就是在两个Collection之间定义的一个关联关系,暂不支持级联的保存功能 例子:一个Person对象有多个Book对象,一对多关系 实体Person public ...

  5. apple 下安装mysql 以及 碰到的问题

    直接说过程: 1.从mysql 官网下载 mysql 5.7最新版:有 两种 一种是 dmg 的傻瓜包,一种是tar.gz的压缩包.我下载的是tar.gz的压缩包. 2.下载成功后,先试用命令sudo ...

  6. zabbix fast

    源码安装步骤 来源它站: 官方文档:https://www.zabbix.com/documentation/2.4/manual/installation/install#from_the_sour ...

  7. linux系统PKWindows系统,从各方便分析linux和Windows的优劣

    服务器系统linux系统和linux系统哪个好用,公说公有理婆说婆有理,今天鼎峰凡凡大概对Linux系统与Windows系统的优缺点PK!可以从以下几个方面来看 ①成本  赞成Linux的声音Linu ...

  8. CentOS随笔 - 1.虚拟机VMware安装CentOS7系统

    前言 转帖请注明出处: http://www.cnblogs.com/Troy-Lv5/ 需要安装CentOS首先你得下载安装镜像文件(地址: https://www.centos.org/downl ...

  9. js构建类的方法

    Javascript是一种基于对象的语言,你遇到的所有东西几乎都是对象.但是,它又不是一种真正的面向对象编程(OOP)语言,因为它的语法中没有Class.(不过,ES6引入了Class这个概念,作为对 ...

  10. iOS设计模式 - 适配器

    iOS设计模式 - 适配器 效果 说明 1. 为了让客户端尽可能的通用,我们使用适配器模式来隔离客户端与外部参数的联系,只让客户端与适配器通信. 2. 本教程实现了适配器模式的类适配器与对象适配器两种 ...