把数据库中有关枚举项值的数字字符串转换成文字字符串

news/2024/7/7 17:11:17
原文:把数据库中有关枚举项值的数字字符串转换成文字字符串

 

标题可能无法表达我的本意。比如,有这样一个枚举:

 

    public enum MyChoice
    {
        MyFirstChoice = 0,
        MySecondChoice =1,
        MyThirdChoice = 2
    }

 

数据库中,某表某字段保存值为"0,1,2",在显示的时候,我们希望是"第一个选择,第二个选择,第三个选择"。如何做呢?

 

可以为枚举项上面标注自定义特性。先自定义一个特性如下:

 

    public class EnumDisplayNameAttribute : Attribute
    {
        private string _displayName;
        public EnumDisplayNameAttribute(string displayName)
        {
            _displayName = displayName;
        }
        public string DisplayName
        {
            get
            {
                return _displayName;
            }
        }
    }
 

 

然后,把自定义特性标注放到枚举项上去。

 

    public enum MyChoice
    {
        [EnumDisplayName("我的第一个选择")]
        MyFirstChoice = 0,
        [EnumDisplayName("我的第二个选择")]
        MySecondChoice =1,
        [EnumDisplayName("我的第三个选择")]
        MyThirdChoice = 2
    }  
 

 

现在,需要一个帮助方法,能读出枚举项上的自定义特性EnumDisplayName。

 

   public class EnumExt
    {
        /// <summary>
        /// 获取枚举项的注释
        /// </summary>
        /// <param name="e">枚举项</param>
        /// <returns></returns>
        public static string GetEnumDescription(object e)
        {
            //获取枚举项
            Type t = e.GetType();
            //获取枚举项的字段
            FieldInfo[] fis = t.GetFields();
            foreach (FieldInfo fi in fis)
            {
                //如果当前字段名称不是当前枚举项
                if (fi.Name != e.ToString())
                {
                    continue;//结束本次循环
                }
                //如果当前字段的包含自定义特性
                if (fi.IsDefined(typeof (EnumDisplayNameAttribute), true))
                {
                    //获取自定义特性的属性值
                    return (fi.GetCustomAttributes(typeof(EnumDisplayNameAttribute), true)[0] as EnumDisplayNameAttribute).DisplayName;
                }
            }
            return e.ToString();
        }
        public static List<SelectListItem> GetSelectList(Type enumType)
        {
            List<SelectListItem> selectList = new List<SelectListItem>();
            //selectList.Add(new SelectListItem{Text = "--请选择--",Value = ""});
            foreach (object e in Enum.GetValues(enumType))
            {
                selectList.Add(new SelectListItem { Text = GetEnumDescription(e), Value = ((int)e).ToString() });
            }
            return selectList;
        }
    }   
 

 

以上,

● GetEnumDescription方法根据枚举项获取其上的自定义特性EnumDisplayNameAttribute的DisplayName属性值。

● GetSelectList方法根据枚举的Type类型返回SelectListItem集合,通常在ASP.NET MVC中使用。

 

最后,就能实现本篇的需求:

 

        static void Main(string[] args)
        {
            string myChoiceInt = "0,1,2";
            string[] choiceArr = myChoiceInt.Split(',');
            string temp = string.Empty;
            foreach (string item in choiceArr)
            {
                //转换成枚举的类型
                short enumValShort = short.Parse(item);
                temp = temp + EnumExt.GetEnumDescription((MyChoice)enumValShort) + ",";
            }
            Console.WriteLine(temp.Substring(0, temp.Length - 1));
            Console.ReadKey();
        }


http://lihuaxi.xjx100.cn/news/245184.html

相关文章

阿里注册中心nacos使用整合Dubbo-原创

阿里注册中心nacos是今年开源的框架&#xff0c;一开始以为就是个zk。后面看了图才明白他对标的竟然是consul\eureka&#xff0c;最重要是完美支持dubbo。我想今年开源它也是别有用意 。&#xff08;目前nacos0.7版本&#xff09; Dubbo 融合 Nacos 成为注册中心 Nacos 作为 Du…

Java调用C/C++编写的第三方dll动态链接库(zz)

这里主要用的方法是JNI。在网上查资料时看到很多人说用JNI非常的复杂&#xff0c;不仅要看很多的文档&#xff0c;而且要非常熟悉C/C编程。恐怕有很多人在看到诸如此类的评论时已经决定绕道用其他方法了。本文将做详细的介绍。AD&#xff1a;51CTO网 首届中国APP创新评选大赛火…

一张图带你了解 Spring Cloud 微服务架构!

点击上方“搜云库技术团队”&#xff0c;选择“设为星标”回复“1024”或“面试题”获取4T学习资料FeignEurekaRibbonHystrixZuulConfigZipkin其它Spring cloud 作为当下主流的微服务框架&#xff0c;让我们实现微服务架构简单快捷Spring cloud中各个组件在微服务架构中扮演的角…

nutch如何发布插件

为什么80%的码农都做不了架构师&#xff1f;>>> 1.修改插件&#xff0c;在原有的插件上修改&#xff0c;比如parse-html插件上修改。 2.修改插件之后&#xff0c;把第三方的包放到/nutch/runtime/local/lib下&#xff08;经测试&#xff0c;只有在此目录下&#xf…

第二十章:异步和文件I/O.(十三)

通过该开销&#xff0c;可以开始实际编写应用程序。 TextFileAsyncPage的XAML文件与TextFileTryoutPage相同&#xff0c;但必须将代码隐藏文件设置为使用异步文件I / O方法。 必须在此处捕获文件I / O函数中可能发生的任何异常&#xff0c;这意味着任何可以抛出异常的方法必须与…

LeetCode - 34. Search for a Range

34. Search for a Range Problems Link ---------------------------------------------------------------------------- Mean: 给定一个有序数组和一个数k&#xff0c;求k在这个数组中的起始下标和结束下标. analyse: 二分查找. Time complexity: O(N) view code /***…

开发三年,如何摆脱日复一日的CRUD?

IT行业技术人员的薪资向来不低&#xff0c;吸引了一批又一批明知“苦逼”又前赴后继的毕业生。作为一名技术从业者&#xff0c;本应该崇拜技术&#xff0c;在商业项目中不断在纵向领域中做深度打磨。但在绝大多数中小型的公司中&#xff0c;业务线要求我们快速迭代&#xff0c;…

win7安装mysql-8.0.13-winx64

这里展示一下&#xff0c;由于需要安装一个版本测试一下数据&#xff0c;其实就是超简单的啦。 下包 注:https://dev.mysql.com/downloads/mysql/ 解压与配置 [mysqld] basedirC:\\Users\\hp\\Downloads\\mysql-8.0.13-winx64 datadirC:\\Users\\hp\\Downloads\\mysql-8.0.13-w…