欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#獲取Description特性的擴(kuò)展類詳解

 更新時(shí)間:2022年06月24日 08:19:36   作者:time-flies  
這篇文章主要和大家詳細(xì)介紹一下C#獲取Description特性的擴(kuò)展類,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定的幫助,需要的可以參考一下

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的代碼示例

    這篇文章主要介紹了C#使用SqlConnection連接到SQL Server的代碼示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • c#?定期重啟程序操作的實(shí)現(xiàn)

    c#?定期重啟程序操作的實(shí)現(xiàn)

    本文主要介紹了c#?定期重啟程序操作的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-09-09
  • C#基于百度AI實(shí)現(xiàn)機(jī)器翻譯功能

    C#基于百度AI實(shí)現(xiàn)機(jī)器翻譯功能

    眾所周知,基于百度ai開發(fā)平臺(tái)我們可以實(shí)現(xiàn)了人臉識(shí)別、文字識(shí)別 、語音識(shí)別等功能。本文將介紹它的另一個(gè)功能,即實(shí)現(xiàn)機(jī)器翻譯,感興趣的可以了解一下
    2022-01-01
  • WPF實(shí)現(xiàn)圖片合成或加水印的方法【2種方法】

    WPF實(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)代碼

    解析美國東部時(shí)間與北京時(shí)間相互轉(zhuǎn)換的實(shí)現(xiàn)代碼

    本篇文章是對(duì)美國東部時(shí)間與北京時(shí)間相互轉(zhuǎn)換的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 詳解二維碼生成工廠

    詳解二維碼生成工廠

    本篇文章主要分享的是3個(gè)免費(fèi)的二維碼接口的對(duì)接代碼和測(cè)試得出的注意點(diǎn)及區(qū)別。具有很好的參考價(jià)值,需要的朋友一起來看下吧
    2016-12-12
  • .net實(shí)現(xiàn)裁剪網(wǎng)站上傳圖片的方法

    .net實(shí)現(xiàn)裁剪網(wǎng)站上傳圖片的方法

    這篇文章主要介紹了.net實(shí)現(xiàn)裁剪網(wǎng)站上傳圖片的方法,比較實(shí)用的功能,需要的朋友可以參考下
    2014-07-07
  • C#如何實(shí)現(xiàn)調(diào)取釘釘考勤接口的功能

    C#如何實(shí)現(xiàn)調(diào)取釘釘考勤接口的功能

    這篇文章主要介紹了C#如何實(shí)現(xiàn)調(diào)取釘釘考勤接口的功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • C#實(shí)現(xiàn)漢字轉(zhuǎn)拼音(多音字)功能詳解

    C#實(shí)現(xiàn)漢字轉(zhuǎn)拼音(多音字)功能詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)漢字轉(zhuǎn)拼音(支持多音字)的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-02-02
  • 深入理解C#指針之美

    深入理解C#指針之美

    在C#中,有時(shí)候希望通過指針來操作內(nèi)存,這樣可以提高效率。我們可以用unsafe關(guān)鍵字修飾含有指針操作的程序段,感興趣的小伙伴可以參考一下,希望可以幫到你
    2021-07-07

最新評(píng)論