C#獲取Description特性的擴(kuò)展類詳解
C#中Description特性主要用于枚舉和屬性,方法比較簡(jiǎn)單,記錄一下以便后期使用。
擴(kuò)展類DescriptionExtension代碼如下:
using System; using System.ComponentModel; using System.Reflection; /// <summary> /// 描述特性的讀取擴(kuò)展類 /// </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("測(cè)試枚舉名")] enum TestEnum { [Description("測(cè)試")] Test1 } [Description("測(cè)試類名")] class TestClass { [Description("測(cè)試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));
補(bǔ)充:
C#利用擴(kuò)展方法獲得枚舉的Description
/// <summary> /// 擴(kuò)展方法,獲得枚舉的Description /// </summary> /// <param name="value">枚舉值</param> /// <param name="nameInstead">當(dāng)枚舉值沒有定義DescriptionAttribute,是否使用枚舉名代替,默認(rèn)是使用</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; }
隨便整一個(gè)枚舉
public enum ModuleType { /// <summary> /// 中國 /// </summary> [Description("中國")] ZH = 1, /// <summary> /// 美國 /// </summary> [Description("美國")] MG = 2 }
到此這篇關(guān)于C#獲取Description特性的擴(kuò)展類詳解的文章就介紹到這了,更多相關(guān)C#獲取Description特性內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用SqlConnection連接到SQL Server的代碼示例
這篇文章主要介紹了C#使用SqlConnection連接到SQL Server的代碼示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03C#基于百度AI實(shí)現(xiàn)機(jī)器翻譯功能
眾所周知,基于百度ai開發(fā)平臺(tái)我們可以實(shí)現(xiàn)了人臉識(shí)別、文字識(shí)別 、語音識(shí)別等功能。本文將介紹它的另一個(gè)功能,即實(shí)現(xiàn)機(jī)器翻譯,感興趣的可以了解一下2022-01-01WPF實(shí)現(xiàn)圖片合成或加水印的方法【2種方法】
這篇文章主要介紹了WPF實(shí)現(xiàn)圖片合成或加水印的方法,結(jié)合實(shí)例形式分析了2種比較實(shí)用的WPF圖片操作相關(guān)技巧,需要的朋友可以參考下2017-03-03解析美國東部時(shí)間與北京時(shí)間相互轉(zhuǎn)換的實(shí)現(xiàn)代碼
本篇文章是對(duì)美國東部時(shí)間與北京時(shí)間相互轉(zhuǎn)換的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05.net實(shí)現(xiàn)裁剪網(wǎng)站上傳圖片的方法
這篇文章主要介紹了.net實(shí)現(xiàn)裁剪網(wǎng)站上傳圖片的方法,比較實(shí)用的功能,需要的朋友可以參考下2014-07-07C#如何實(shí)現(xiàn)調(diào)取釘釘考勤接口的功能
這篇文章主要介紹了C#如何實(shí)現(xiàn)調(diào)取釘釘考勤接口的功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08C#實(shí)現(xiàn)漢字轉(zhuǎn)拼音(多音字)功能詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)漢字轉(zhuǎn)拼音(支持多音字)的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2023-02-02