System.Reflection 获取描述

时间:2021-08-09 20:01:46

我们需要获取类,属性,方法的描述。这个跟获取枚举的描述一样,需要我们通过反射来做。这还需要我们的利用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();

System.Reflection  获取描述

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