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 ...
随机推荐
- 收藏的几个关于php面向对象教程
面向对象的分析方法是利用面向对象的信息建模概念,如实体.关系.属性等,同时运用封装.继承.多态等机制来构造模拟现实系统的方法,学会了面向对象思想,能够大大提高php编程开发效率!本篇文章php中文网将 ...
- AngularJS开发人员最常犯的10个错误
简介AngularJS是目前最为活跃的Javascript框架之一,AngularJS的目标之一是简化开发过程,这使得AngularJS非常善于构建小型app原型,但AngularJS对于全功能的客户 ...
- OSMC Vs. OpenELEC Vs. LibreELEC – Kodi Operating System Comparison
Kodi's two slim-and-trim kid brothers LibreELEC and OpenELEC were once great solutions for getting t ...
- web测试流程的总结及关注点
项目的测试流程大只包含的几个阶段:立项.需求评审.用例评审.测试执行.测试报告文档 一.立项后测试需要拿到的文档 1.需求说明书 2.原型图(及UI图) 3.接口文档 4.数据库字典(表的数量.缓存机 ...
- vue3.0环境最新安装步骤
安装最新的node.js版本: https://nodejs.org/dist/v8.11.3/node-v8.11.3-x64.msi 安装vue: npm install -g @vue/cli ...
- Zip压缩/解压缩(文件夹)
#PS2.0压缩为.zip文件: $zip = "D:\audit_log\test.zip"New-Item $zip -ItemType file$shellApplicati ...
- Python学习---range/for/break/continue简单使用
range的使用:注意,在python3中,交互模式下已经不显示了 for循环的使用 打印50-70 # 第一种方案 for i in range(100): if i <= 70 and i ...
- Exchange Server 2016 管理邮箱收发限制
备注:本文是Exchange Server 2016管理系列的配套课件,更加详细的讲解请参考视频课程,文章结尾有视频课程主页的链接. 进行收发邮件大小的限制是很有必要的,因为邮件服务器不能当作文件服务 ...
- 以太网的 MAC 层
一.MAC 层的硬件地址 在局域网中,主机的硬件地址又称为物理地址,或 MAC 地址.6个字节. IEEE 的注册管理机构 RA 负责向厂家分配地址字段的前三个字节(即高位 24 位,组织唯一标识符O ...
- 连续支付的年金(continuously payable annuity)
一.含义 假设连续不断地付款,但每年的付款总量仍然为1元. 二. 连续支付年金是年支付次数m趋于无穷大时的年金,故 连续支付年金与基本年金的关系: 连续支付,每年的支付总量为1,支付期限为无穷: 积累 ...