首页 > 编程语言 >C# 枚举高级用法之Description

C# 枚举高级用法之Description

时间:2023-09-25 15:26:09浏览次数:35  
标签:Description val C# fish 枚举 customAttribute DataTest public

基础枚举

public enum Anima{

//默认值dog =0, 依次往下排,可自定义fox = 1, fish =2,tiger=3

   dog,

   fox,

      fish,

   tiger

}

高级用法,反射元数据

加了Description,就可以通过反射访问这些数据

public enum Anima{

//默认值dog =0, 依次往下排,可自定义fox = 1, fish =2,tiger=3

  [Description("狗狗")]

  [DataTest("汪汪")]

   dog,

     [Description("狐狸")]

   [DataTest("吱吱")]

   fox,

    [Description("鱼")]

   [DataTest("吐泡泡")]

      fish,

   [Description("老虎")]

   [DataTest("嗷呜")]

   tiger

}

//要多添加一个特性,可以加一个继承自Attribute的类

public class DataTest : Attribute

{

    public string Data{get;set;}

    public DataTest(){

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

}

标签:Description,val,C#,fish,枚举,customAttribute,DataTest,public
From: https://www.cnblogs.com/KevinSteven/p/17727932.html

相关文章

  • OGG MA - Not Able To Log InAdmin server ERROR: User name 'oggadmin' or password
    ogg的密码文件可能会损坏需要修复就新建一个新的ogg微服务并且把密码文件考到问问题的地方进行覆盖,我觉得这个是一个bugTherecommendationistoCreateadummyMAinstallationonthesameserverordifferenttestserver.Thencopythewallet[cwallet.sso]fromthis......
  • JOISC做题记录
    题目真的很好!!!所以来写一写。但都是一句话题解,因为我实在很懒。打*的是没独立做出来的。慢慢补,不急2023Day1T1TwoCurrencies签到题。主席树上二分就行。$O((n+Q)\logn)$。*2023Day1T2FestivalsinJOIKingdom2还不会。2023Day1T3Passport走遍$1\simn$显然......
  • VMware ESXi 8.0U2 macOS Unlocker & OEM BIOS 标准版和厂商定制版
    VMwareESXi8.0U2macOSUnlocker&OEMBIOS标准版和厂商定制版ESXi8.0U2标准版,Dell(戴尔)、HPE(慧与)、Lenovo(联想)、Inspur(浪潮)等定制版请访问原文https://sysin.org/blog/vmware-esxi-8-u2-oem/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org2023-......
  • 蓝天采集器开启自动采集设置PHP-CLI(命令行)
    宝塔面板操作打开终端,进ssh执行以下两条命令:rm-f/usr/bin/phpln-sf/www/server/php/70/bin/php/usr/bin/php最后,测试是否成功打开命令行,输入php-v 服务器这边就配置好了,我们接下来回到我们的蓝天采集器后台,点击设置-----采集设置-----采集运行模式-----选择cli......
  • VMware ESXi 8.0U2 macOS Unlocker & OEM BIOS 集成网卡驱动和 NVMe 驱动 (集成驱动版
    VMwareESXi8.0U2macOSUnlocker&OEMBIOS集成网卡驱动和NVMe驱动(集成驱动版)发布ESXi8.0U2集成驱动版,在个人电脑上运行企业级工作负载请访问原文链接:https://sysin.org/blog/vmware-esxi-8-u2-sysin/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org发布......
  • @ConfigurationProperties配置类的新操作
    对应的yml文件:logging:level:org.springframework.web:INFOcom.github.binarywang.demo.wx.mp:DEBUGme.chanjar.weixin:DEBUGwx:mp:useRedis:falseredisConfig:host:127.0.0.1port:6379configs:-appId:1111......
  • python 缓存机制如何实现(cacheout)
    Python缓存机制可以使用第三方库cacheout来实现。cacheout提供了一个Cache类,它支持多种缓存策略,包括LRU、FIFO、LFU和TTL。Cache类的基本使用方法如下:1.安装cacheout:pipinstallcacheout2.导入Cache类:fromcacheoutimportCache3.创建Cache对象:cac......
  • Applescript脚本实现全自动无痕检测手机号码是否注册iMessage的原理
    一、检测数据的两种方式:1.人工筛选,将要验证的号码输出到文件中,以逗号分隔。再将文件中的号码粘贴到iMessage客户端的地址栏,iMessage客户端会自动逐个检验该号码是否为iMessage账号,检验速度视网速而定。红色表示不是iMessage账号,蓝色表示iMessage账号。2.编写脚本控制Macos/iphon......
  • C语言统计数组里面各个元素出现的次数
    #include<iostream>#include<stdio.h>intmain(){intnums[]={1,1,2,2,3,4,5,6,6};intsize=sizeof(nums)/sizeof(nums[0]);//创建一个全0的空数组int*counterNums=(int*)calloc(size,sizeof(int));for(inti=......
  • 一文带你实现云上部署轻量化定制表单Docker
    本文分享自华为云社区 《【华为云云耀云服务器L实例评测|云原生】自定制轻量化表单Docker快速部署云耀云服务器|玩转华为云》,作者:计算机魔术师。华为云的云耀云服务器L实例备受推崇,以其高效、可靠和安全的基础设施服务而闻名。本文将为展示在该服务器上部署轻量化定制表单服务,......