日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 枚举高级用法之Description

發布時間:2023/12/14 C# 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 枚举高级用法之Description 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

c# 枚舉高級用法之

基礎枚舉

namespace TestEnum {//聲明public enum Name{//默認值 boob = 0 ,依次往下排,可自定義 jackMa = 3,jackCHan那就= 4boob,JackMa,JackChan,//Lisa,Poro} }

聲明枚舉,調用。
幾個常用的轉換不做介紹,一嗦一大堆。

  • enum轉string
  • enum 轉int
  • string轉enum
  • 整型轉enum
  • 高級點用法,反射元數據

    public enum Name{[Description("鮑勃")]boob,[Description("馬宇")]JackMa,[Description("陳龍")]JackChan,[Description("麗莎")]Lisa,[Description("保羅")]Poro}

    加了Description,就可以通過反射來訪問這些數據
    如何訪問?

    using System; using System.ComponentModel; namespace TestEnum {//基礎枚舉public enum Name{[Description("鮑勃")]boob,[Description("馬宇")]JackMa,[Description("陳龍")]JackChan,[Description("麗莎")]Lisa,[Description("保羅")]Poro}//主要這個GetDescription()方法,看不懂沒關系,如何調用static class EnumExtensions{public static string GetDescription(this Enum val){var field = val.GetType().GetField(val.ToString());var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));return customAttribute == null ? val.ToString() : ((DescriptionAttribute)customAttribute).Description;}} }

    調用

    string str = Name.boob.GetDescription(); //需要調用的地方調用

    一個特性不夠,能再加一個嗎?
    可以
    加一個繼承自Attribute 的類

    public class DataTest : Attribute{public string Data { get; set; }public DataTest(string data){Data = data;}public DataTest(){}}

    加完了自定義特性后你就會發現可以加了。。

    [Description("鮑勃")][DataTest("中文3級")]boob,[Description("馬宇")][DataTest("中文2級")]JackMa,[Description("陳龍")][DataTest("中文1級")]JackChan,[Description("麗莎")][DataTest("中文2級")]Lisa,[Description("保羅")][DataTest("中文2級")]Poro

    就變成了這樣
    訪問也得加一個

    public static string GetData(this Enum val){var field = val.GetType().GetField(val.ToString());var customAttribute = Attribute.GetCustomAttribute(field, typeof(DataTest));return customAttribute == null ? val.ToString() : ((DataTest)customAttribute).Data;}

    調用

    string str = Name.boob.GetDescription();string DStr = Name.boob.GetData();

    以上就是enum 加+自定義特性
    不夠可以隨時自己加。
    可以測試玩玩,據說反射浪費資源。

    完整代碼

    using System; using System.ComponentModel; namespace TestEnum {//基礎枚舉public enum Name{[Description("鮑勃")][DataTest("中文3級")]boob,[Description("馬宇")][DataTest("中文2級")]JackMa,[Description("陳龍")][DataTest("中文1級")]JackChan,[Description("麗莎")][DataTest("中文2級")]Lisa,[Description("保羅")][DataTest("中文2級")]Poro}public class DataTest : Attribute{public string Data { get; set; }public DataTest(string data){Data = data;}public DataTest(){}}static class EnumExtensions{public static string GetDescription(this Enum val){var field = val.GetType().GetField(val.ToString());var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));return customAttribute == null ? val.ToString() : ((DescriptionAttribute)customAttribute).Description;}public static string GetData(this Enum val){var field = val.GetType().GetField(val.ToString());var customAttribute = Attribute.GetCustomAttribute(field, typeof(DataTest));return customAttribute == null ? val.ToString() : ((DataTest)customAttribute).Data;}} }

    PS:可以聲明obect類型Attribute.子類。 那。。。。。可發揮空間就大了。

    總結

    以上是生活随笔為你收集整理的C# 枚举高级用法之Description的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。