0

0

C# 枚举操作工具类

黄舟

黄舟

发布时间:2017-02-18 10:29:27

|

1472人浏览过

|

来源于php中文网

原创

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;

namespace JianKunKing.Common.EnumClass
{
    /// 
    /// 枚举操作类
    /// 
    public static class EnumOperation
    {
        #region 从枚举中获取Description
        /// 
        /// 从枚举中获取Description
        /// 
        /// 需要获取枚举描述的枚举
        /// 描述内容
        public static string GetDescription(Enum enumName)
        {
            try
            {
                string _description = string.Empty;
                FieldInfo _fieldInfo = enumName.GetType().GetField(enumName.ToString());
                DescriptionAttribute[] _attributes = GetDescriptAttr(_fieldInfo);
                if (_attributes != null && _attributes.Length > 0)
                {
                    _description = _attributes[0].Description;
                }
                else
                {
                    _description = enumName.ToString();
                }
                return _description;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region 获取字段Description(private)
        /// 
        /// 获取字段Description
        /// 
        /// FieldInfo
        /// DescriptionAttribute[] 
        private static DescriptionAttribute[] GetDescriptAttr(this FieldInfo fieldInfo)
        {
            try
            {
                if (fieldInfo != null)
                {
                    return (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
                }
                return null;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region 根据Description获取枚举定义字符串
        /// 
        /// 根据Description获取枚举定义字符串
        /// 
        /// 枚举类型
        /// 枚举描述
        /// 枚举
        public static T GetEnumName(string description)
        {
            Type _type = typeof(T);
            foreach (FieldInfo field in _type.GetFields())
            {
                DescriptionAttribute[] _curDesc = field.GetDescriptAttr();
                if (_curDesc != null && _curDesc.Length > 0)
                {
                    if (_curDesc[0].Description == description)
                    {
                        return (T)field.GetValue(null);
                    }
                }
                else
                {
                    if (field.Name == description)
                    {
                        return (T)field.GetValue(null);
                    }
                }
            }
            throw new ArgumentException(string.Format("{0} 未能找到对应的枚举.", description), "Description");
        }
        #endregion

        #region 将枚举转换为ArrayList
        /// 
        /// 将枚举转换为ArrayList
        /// 说明:
        /// 若不是枚举类型,则返回NULL
        /// 
        /// 枚举类型
        /// ArrayList
        public static ArrayList ToArrayList(Type type)
        {
            try
            {
                if (type.IsEnum)
                {
                    ArrayList _array = new ArrayList();
                    Array _enumValues = Enum.GetValues(type);
                    foreach (Enum value in _enumValues)
                    {
                        _array.Add(new KeyValuePair(value, GetDescription(value)));
                    }
                    return _array;
                }
                return null;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region 根据枚举值得到属性Description中的描述, 如果没有定义此属性则返回空串
        ///   
        /// 根据枚举值得到属性Description中的描述, 如果没有定义此属性则返回空串  
        ///   
        ///   
        ///   
        ///   
        public static String GetEnumDescriptionString(int value, Type enumType)
        {
            try
            {
                NameValueCollection nvc = GetNVCFromEnumValue(enumType);
                return nvc[value.ToString()];
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region 根据枚举类型得到其所有的值 与 枚举定义Description属性 的集合
        ///   
        /// 根据枚举类型得到其所有的 值 与 枚举定义Description属性 的集合  
        ///   
        ///   
        ///   
        public static NameValueCollection GetNVCFromEnumValue(Type enumType)
        {
            try
            {
                NameValueCollection nvc = new NameValueCollection();
                Type typeDescription = typeof(DescriptionAttribute);
                System.Reflection.FieldInfo[] fields = enumType.GetFields();
                string strText = string.Empty;
                string strValue = string.Empty;
                foreach (FieldInfo field in fields)
                {
                    if (field.FieldType.IsEnum)
                    {
                        strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
                        object[] arr = field.GetCustomAttributes(typeDescription, true);
                        if (arr.Length > 0)
                        {
                            DescriptionAttribute aa = (DescriptionAttribute)arr[0];
                            strText = aa.Description;
                        }
                        else
                        {
                            strText = "";
                        }
                        nvc.Add(strValue, strText);
                    }
                }
                return nvc;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region 根据枚举值得到相应的枚举定义字符串
        ///   
        ///根据枚举值得到相应的枚举定义字符串  
        ///   
        ///   
        ///   
        ///   
        public static String GetEnumString(int value, Type enumType)
        {
            try
            {
                NameValueCollection nvc = GetEnumStringFromEnumValue(enumType);
                return nvc[value.ToString()];
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region 根据枚举类型得到其所有的 值 与 枚举定义字符串 的集合
        ///   
        /// 根据枚举类型得到其所有的 值 与 枚举定义字符串 的集合  
        ///   
        ///   
        ///   
        public static NameValueCollection GetEnumStringFromEnumValue(Type enumType)
        {
            try
            {
                NameValueCollection nvc = new NameValueCollection();
                Type typeDescription = typeof(DescriptionAttribute);
                System.Reflection.FieldInfo[] fields = enumType.GetFields();
                string strText = string.Empty;
                string strValue = string.Empty;
                foreach (FieldInfo field in fields)
                {
                    if (field.FieldType.IsEnum)
                    {
                        strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
                        nvc.Add(strValue, field.Name);
                    }
                }
                return nvc;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion
    }
}

如果知道枚举的编号就是那个int类型的数字,获取name直接强转就可以 (枚举类型)(int值)

小注:
       对于基础公共方法类来说,其中最好不是获取异常直接抛出,切记不要吞掉或者只抛出部分信息。

 以上就是C#  枚举操作工具类的内容,更多相关内容请关注PHP中文网(www.php.cn)!

中英双语红色大气外贸企业网站源码1.1
中英双语红色大气外贸企业网站源码1.1

注意:需要在本地调试我们的网站的必须安装配置IIS,不可以使用ASP调试工具.exe或小旋风asp或APMServ等这类工具调试,因为这类简易的IIS替代工具,去掉了很多功能,有些语句是不支持的。 【程序】ASP 【数据库】ACCESS (只要支持ASP的空间均自带此数据库) 【前台】全部生成.html静态页面 本程序专为企业网站进行打造,三大特色无与伦比: ☆全后台操作☆前台所有内容均可以后台

下载


热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
C++ 设计模式与软件架构
C++ 设计模式与软件架构

本专题深入讲解 C++ 中的常见设计模式与架构优化,包括单例模式、工厂模式、观察者模式、策略模式、命令模式等,结合实际案例展示如何在 C++ 项目中应用这些模式提升代码可维护性与扩展性。通过案例分析,帮助开发者掌握 如何运用设计模式构建高质量的软件架构,提升系统的灵活性与可扩展性。

14

2026.01.30

c++ 字符串格式化
c++ 字符串格式化

本专题整合了c++字符串格式化用法、输出技巧、实践等等内容,阅读专题下面的文章了解更多详细内容。

9

2026.01.30

java 字符串格式化
java 字符串格式化

本专题整合了java如何进行字符串格式化相关教程、使用解析、方法详解等等内容。阅读专题下面的文章了解更多详细教程。

12

2026.01.30

python 字符串格式化
python 字符串格式化

本专题整合了python字符串格式化教程、实践、方法、进阶等等相关内容,阅读专题下面的文章了解更多详细操作。

4

2026.01.30

java入门学习合集
java入门学习合集

本专题整合了java入门学习指南、初学者项目实战、入门到精通等等内容,阅读专题下面的文章了解更多详细学习方法。

20

2026.01.29

java配置环境变量教程合集
java配置环境变量教程合集

本专题整合了java配置环境变量设置、步骤、安装jdk、避免冲突等等相关内容,阅读专题下面的文章了解更多详细操作。

18

2026.01.29

java成品学习网站推荐大全
java成品学习网站推荐大全

本专题整合了java成品网站、在线成品网站源码、源码入口等等相关内容,阅读专题下面的文章了解更多详细推荐内容。

19

2026.01.29

Java字符串处理使用教程合集
Java字符串处理使用教程合集

本专题整合了Java字符串截取、处理、使用、实战等等教程内容,阅读专题下面的文章了解详细操作教程。

3

2026.01.29

Java空对象相关教程合集
Java空对象相关教程合集

本专题整合了Java空对象相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.29

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
C# 教程
C# 教程

共94课时 | 8万人学习

C 教程
C 教程

共75课时 | 4.3万人学习

C++教程
C++教程

共115课时 | 14.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号