C#获取Description特性的扩展类详解
作者:time-flies 发布时间:2022-06-04 13:27:24
标签:C#,Description,特性
C#中Description特性主要用于枚举和属性,方法比较简单,记录一下以便后期使用。
扩展类DescriptionExtension代码如下:
using System;
using System.ComponentModel;
using System.Reflection;
/// <summary>
/// 描述特性的读取扩展类
/// </summary>
public static class DescriptionExtension
{
/// <summary>
/// 获取枚举的描述信息
/// </summary>
public static string GetDescription(this Enum em)
{
Type type = em.GetType();
FieldInfo fd = type.GetField(em.ToString());
string des = fd.GetDescription();
return des;
}
/// <summary>
/// 获取属性的描述信息
/// </summary>
public static string GetDescription(this Type type, string proName)
{
PropertyInfo pro = type.GetProperty(proName);
string des = proName;
if (pro != null)
{
des = pro.GetDescription();
}
return des;
}
/// <summary>
/// 获取属性的描述信息
/// </summary>
public static string GetDescription(this MemberInfo info)
{
var attrs = (DescriptionAttribute[])info.GetCustomAttributes(typeof(DescriptionAttribute), false);
string des = info.Name;
foreach (DescriptionAttribute attr in attrs)
{
des = attr.Description;
}
return des;
}
}
使用方法:
[Description("测试枚举名")]
enum TestEnum
{
[Description("测试")]
Test1
}
[Description("测试类名")]
class TestClass
{
[Description("测试class")]
public int Test1 { get; set; }
}
//获取枚举类型的描述特性
string str1 = typeof(TestEnum).GetDescription();
//获取枚举值的描述特性
string str2 =TestEnum.Test1.GetDescription();
str2 = typeof(TestEnum).GetDescription(nameof(TestEnum.Test1));
str2 = typeof(TestEnum).GetDescription(TestEnum.Test1.ToString());
//获取类的描述特性
string str3 = typeof(TestClass).GetDescription();
//获取属性的描述特性(也可以反射遍历属性列表)
string str4 = typeof(TestClass).GetDescription();
TestClass test = new TestClass();
str4 = typeof(TestClass).GetDescription(nameof(test.Test1));
补充:
C#利用扩展方法获得枚举的Description
/// <summary>
/// 扩展方法,获得枚举的Description
/// </summary>
/// <param name="value">枚举值</param>
/// <param name="nameInstead">当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用</param>
/// <returns>枚举的Description</returns>
public static string GetDescription(this Enum value, Boolean nameInstead = true)
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name == null)
{
return null;
}
FieldInfo field = type.GetField(name);
DescriptionAttribute attribute = System.Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute == null && nameInstead == true)
{
return name;
}
return attribute?.Description;
}
随便整一个枚举
public enum ModuleType
{
/// <summary>
/// 中国
/// </summary>
[Description("中国")]
ZH = 1,
/// <summary>
/// 美国
/// </summary>
[Description("美国")]
MG = 2
}
来源:https://www.cnblogs.com/timefiles/p/16395131.html


猜你喜欢
- @ConfigurationProperties作用:用于获取配置文件中的属性定义并绑定到javaBean属性中举个栗子:配置文件mycar
- 使用自定义注解实现接口限流在高并发系统中,保护系统的三种方式分别为:缓存,降级和限流。限流的目的是通过对并发访问请求进行限速或者一个时间窗口
- 前言缓冲区 又称为缓存,它是内存空间的一部分。也就是说,在内存空间中预留了一定的存储空间,这些存储空间用来缓冲输入或输出的数据,这部分预留的
- 本文实例讲述了Android获取SD卡及手机ROM容量的方法。分享给大家供大家参考,具体如下:这里通过一个简单的小例子,来获取SD卡的容量和
- 下面本文将针对以上几点问题进行描述讨论,我们就以“中文”两个字为例来说明,查找相关资料可知“中文”的GB2312编码是“d6d0 cec4”
- 话不多说,直接上实例:一、获取集合内重复值public void GetDuplicateValue(){ List<st
- 1 前言在前文中,已经讲述了 AOP 的后置处理器使用和方法,在本文中继续分享增强信息相关的源码,这里才是 AOP 的核心代码。2 spri
- Android绘图常用方法有哪些,下面一一为大家列举:1、有关画笔(Paint)的方法Paint mPaint= new Paint();m
- 在分布式系统架构中,如果一个应用不能对来自依赖的故障进行隔离,那该应用本身就处在被拖垮的风险中。 因此,为了构建稳定、可靠的分布式系统,我们
- Java 中的 CyclicBarrier 是一种同步工具,它可以让多个线程在一个屏障处等待,直到所有线程都到达该屏障处后,才能继续执行。C
- 本文实例为大家分享了Android实现滑动屏幕切换图片的具体代码,供大家参考,具体内容如下activity_main.xml 文件代码:&l
- FTP(File Transfer Protocol)就是文件传输协议。通过FTP客户端从远程FTP服务器上拷贝文件到本地计算机称为下载,将
- 1.super介绍我们可以通过super关键字来实现对父类成员的访问,用来引用当前对象的父类。用于访问父类的属性,方法,构造器2.super
- 1.概念1.AOP技术简介AOP 为Aspect Oriented Programming 的缩写,意思为面向切面编程,是通过预编译方式和运
- 日常对于金额计算,应该都是用的BigDecimal,可是苦于没有好的工具类方法,现在贡献一个我正在用的对于数字计算的工具类,项目中就是用的这
- 在Android Studio中,你可以很快速的使用Parcelable插件进行实体类的序列化的实现,使用该插件后,你的实体类可以快速的实现
- 范例说明Android的Widget,有许多是为了与User交互而特别设计的,但也有部分是作为程序提示、显示程序运行状态的Widget。现在
- 其实可以理解Handler为主线程和另外的线程之间进行数据更新的东东,并且Handler在主线程中,并在Handler直接调用线程的run方
- 本文实例讲述了Android编程之canvas绘制各种图形的方法。分享给大家供大家参考,具体如下:1、首先说一下canvas类:Class
- 线索二叉树的意义对于一个有n个节点的二叉树,每个节点有指向左右孩子的指针域。其中会出现n+ 1个空指针域,这些空间不储存任何事物,浪费着内存