System.Reflection 获取描述
我们需要获取类,属性,方法的描述。这个跟获取枚举的描述一样,需要我们通过反射来做。这还需要我们的利用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 获取描述的更多相关文章
- System.Reflection.Assembly.GetEntryAssembly()获取的为当前已加载的程序集
今天在使用System.Reflection.Assembly.GetEntryAssembly()获取程序集时,发现获取的程序集不全.原来是因为C#的程序集为延迟加载,此方法只获取当前已加载的,未加 ...
- 【C#基础】System.Reflection (反射)
在使用.NET创建的程序或组件时,元数据(metadata)和代码(code)都存储于"自成一体"的单元中,这个单元称为装配件.我们可以在程序运行期间访问这些信息.在System. ...
- 基础命名空间:反射 using System.Reflection
反射概念: .Net的应用程序由几个部分:‘程序集(Assembly)’.‘模块(Module)’.‘类型(class)’组成,程序集包含模块 模块包含类型,类型又包含 成员,而反射提供一 ...
- System.Reflection.Emit学习
C#反射发出System.Reflection.Emit学习 分享: 1 一.System.Reflection.Emit概述 Emit,可以称为发出或者产生.与Emit相关的类基本都存在于Syste ...
- System.Reflection.Emit摘记
动态类型在.net中都是用什么类型来表示的.程序集:System.Reflection.Emit.AssemblyBuilder(定义并表示动态程序集)构造函数:System.Reflection.E ...
- 利用system.reflection遍历一个类的变量成员
假设有下面一个类,在程序中已初始化,如何获取里面的变量成员name,age,onduty及其值呢? public class Employee { public string name; public ...
- C#反射发出System.Reflection.Emit学习
一.System.Reflection.Emit概述 Emit,可以称为发出或者产生.与Emit相关的类基本都存在于System.Reflection.Emit命名空间下.反射,我们可以取得形如程序集 ...
- C# System.Reflection.Assembly动态加载资源文件
需求:需要做甘特图的显示,并且在甘特中加载图片.图片太多,写判断代码太多.用反射吧. 核心代码: try { if (stateColour < 0) return null; System.R ...
- 反射基础 System.Reflection
一.获取程序集Assembly 1.获取当前运行的程序集 System.Reflection.Assembly[] asm = AppDomain.CurrentDomain.GetAssemblie ...
随机推荐
- 基于jQuery日历插件制作日历
这篇文章主要介绍了基于jQuery日历插件制作日历的相关资料,需要的朋友可以参考下 来看下最终效果图吧: 是长得丑了一点,不要吐槽我-.- 首先来说说这个日历主要的制作逻辑吧: ·一个月份最多有31天 ...
- JS 写入到文件
//js写文件 function doSave(value, type, name) { var blob; if (typeof window.Blob == "function" ...
- laravel开发之-网站初建
1 cmd 打开电脑命令窗口 2 目录切换到网站根目录 3 输入命令:php artisan serve 4 model 生成命令:php artisan make:model 需要生成的model名 ...
- 使用iview时,页面没了滚动条
场景:页面中有一个确认按钮,保存后弹框预览在点保存按钮,实现数据提交.提交后回到数据列表页,用this.$router.push('list'),返回后页面无法滚动了. 原因:排查后发现弹框时在bod ...
- ASP.NET MVC学习笔记 第一天
MVC:Mode(模型).View(视图).Controller(控制器) 在服务器接收到请求(Request)时,路由(Routing)定义了应该调用的控制器,以及应该调用的控制器动 ...
- MUI框架-02-注意事项-适用场景-实现页面间传值
MUI框架-02-注意事项-适用场景-实现页面间传值 关于开发,我拷贝太多也没什么意义,就请查阅:官方文档:http://dev.dcloud.net.cn/mui/ui/ 快速入门 - 注意事项 有 ...
- HttpStatus
/* * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Vers ...
- Java Spring中@Query中使用JPQL LIKE 写法
两种方式 // 一 public List<TestEntity> searchByJpql(){ String jpql = "select k from TestEntity ...
- oracle 帐号scott被锁定 如何解锁
由于多次输入:账号 密码不对 oracle 帐号scott被锁定 如何解锁: 具体操作步骤如下:C:> sqlplus请输入用户名:sys输入口令:sys as sysdba //注意:在口令这 ...
- php测试工具
如果是测压力有apache的ab如果要看性能则有xdebug和xhprof.还有linux的strace命令来跟踪程序的执行时的系统调用