首页 > 编程语言 >C# Enum使用

C# Enum使用

时间:2023-10-20 14:24:50浏览次数:27  
标签:CommandAccepted Description val C# Enum GetDescription customAttribute 使用

 1     public enum CoEdit
 2     {
 3         
 4         [Description("通讯开始")]
 5         CommunicationStart = 0001,
 6 
 7         [Description("确认通讯开始")]
 8         NotarizeCommunicationStart = 0002,
 9 
10         [Description("通讯停止")]
11         CommunicationStop = 0003,
12 
13         [Description("命令错误")]
14         CommandError = 0004,
15 
16         [Description("命令已接受")]
17         CommandAccepted = 0005
18     }
19     //主要这个GetDescription()方法,看不懂没关系,如何调用
20     static class EnumExtensions
21     {
22         public static string GetDescription(this Enum val)
23         {
24             var field = val.GetType().GetField(val.ToString());
25             var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));
26             return customAttribute == null ? val.ToString() : ((DescriptionAttribute)customAttribute).Description;
27         }
28     } 


 //使用时 

Console.WriteLine(EnumExtensions.GetDescription(CoEdit.CommandAccepted)); 

//结果
命令已接受



标签:CommandAccepted,Description,val,C#,Enum,GetDescription,customAttribute,使用
From: https://www.cnblogs.com/chibangze/p/17776953.html

相关文章

  • error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visua
    error:MicrosoftVisualC++14.0isrequired.Getitwith"BuildToolsforVisualStudio":https://visualstudio.microsoft.com/downloads/ 一、背景说明在编译安装达梦数据库的Python驱动dmPython时,执行编译安装命令如下:pythonsetup.pyinstall 报错信息......
  • 在Java中,可以使用`java.nio.file`包中的`Files`类来进行文件搜索。以下是一种模糊搜索
    在Java中,可以使用`java.nio.file`包中的`Files`类来进行文件搜索。以下是一种模糊搜索文件的方法:```javaimportjava.io.IOException;importjava.nio.file.*;importjava.nio.file.attribute.BasicFileAttributes;publicclassFileSearch{publicstaticvoidmain(String......
  • C# UWP 项目debug发布无问题运行,Release模式闪退问题的解决
    近期解决了一个非常迷惑的一个问题,问题就在于,项目在本地环境运行时没有任何异常和闪退的现象,而且以Debug模式发布后,发布包安装后,运行也没有任何问题,但是当以Release模式发布并安装,就会出现闪退的问题。针对对系统日志的分析和错误的解决,我们一共找到两个原因导致这个问题的出现。......
  • 使用CSS的cross-fade()实现基于url的网页背景图半透明显示
     参考链接:https://www.zhangxinxu.com/wordpress/2020/07/css-cross-fade-background-image-opacity/引子在开发过程中,碰到了一个比较特别的情况,就是使用url()背景图像,同时又希望background-image背景图片是半透明的,作为网页的半透明背景图,这时候使用opacity,如果使用某一个大......
  • lorawan.class a与网关通信错位一次
    我的流程就是先收节点数据再发送 发现修改数据后,都延迟了一次。 根硬件厂家沟通。然后确认了这个。说这是classa的特性。就是延迟错位一次。-------------------------------------目瞪狗呆中-------------------------------------- ......
  • Example: Pandas Excel output with column formatting pandas 对excel 列做格式处理
    AnexampleofconvertingaPandasdataframetoanExcelfilewithcolumnformatsusingPandasandXlsxWriter.Itisn’tpossibletoformatanycellsthatalreadyhaveaformatsuchastheindexorheadersoranycellsthatcontaindatesordatetimes.Note:......
  • Axure RP 10 for mac:打造高保真交互原型设计的终极利器
    AxureRP10formac是一款功能强大的UI/UX设计工具,它可以帮助用户快速创建高保真的交互原型,帮助团队在早期阶段测试并验证产品或界面的想法。→→↓↓载AxureRP10forMacAxureRP10具有丰富的组件库和强大的交互功能,可以模拟各种复杂的交互效果,帮助用户在短时间内创建出令......
  • 实验2 C语言分支与循环基础应用编程
    实验任务1task1.c源代码:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;1314srand(time(0));151......
  • Adobe Acrobat Pro DC 2023:轻松驾驭PDF,开启高效工作新篇章 Mac+win版
    AdobeAcrobatProDC2023是Adobe公司推出的一款强大的PDF文档处理软件,它可以帮助用户轻松管理和编辑PDF文件,同时还提供了丰富的注释、高亮、添加文本等功能,使得用户可以在PDF文件中进行高效的沟通和协作。→→↓↓载AdobeAcrobatProDC2023mac/win版相较于其他PDF处理软......
  • css元素水平垂直居中
    1、单行文字的处理<html><head><style>/*要点,设置行高和DIV的行高一行*/#div{background-color:antiquewhite;height:100px;line-height:100px;text-alig......