【C#】枚举

枚举

复制代码
public static class CommonEnums
    {
        public enum people
        {
            /// <summary>
            ///男人
            /// </summary>
            [Description("男人")]
            man = 1,
            /// <summary>
            /// 女人
            /// </summary>
            [Description("女人")]
            women = 2,
        }
}
复制代码

string->枚举

CommonEnums.people en = (CommonEnums.people)Enum.Parse(typeof(CommonEnums.people), "women");

枚举->Dictionary

复制代码
//使用:生成一个dictionary
Dictionary<int, string> dic=CommonEnums.people.man.ToEnumDictionary();

//静态方法
public static class EnumExtension
    {
        public static Dictionary<int, string> ToEnumDictionary(this Enum enumT, string category = "")
        {
            Type enumType = enumT.GetType();
            if (!enumType.IsEnum) throw new Exception("参数不是枚举");

            var array = enumType.GetEnumValues();
            if (array.Length < 1) return new Dictionary<int, string>(1);
            Dictionary<int, string> result = new Dictionary<int, string>(array.Length);

            int[] enumValues = new int[array.Length];
            int index = 0;

            foreach (var item in array)
            {
                enumValues[index] = Convert.ToInt32(item);
                ++index;
            }

            var enumNames = enumType.GetEnumNames();
            DescriptionAttribute[] descriptions = null;
            CategoryAttribute[] categorys = null;

            for (index = 0; index < array.Length; ++index)
            {
                FieldInfo fi = enumType.GetField(enumNames[index]);
                descriptions = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];

                if (category != string.Empty)
                {
                    categorys = fi.GetCustomAttributes(typeof(CategoryAttribute), false) as CategoryAttribute[];
                    if (categorys != null && categorys.Length > 0)
                    {
                        if (categorys[0].Category != category)
                        {
                            continue;
                        }
                    }
                }

                if (descriptions != null && descriptions.Length > 0)
                {
                    result.Add(enumValues[index], descriptions[0].Description);
                }
                else
                {
                    result.Add(enumValues[index], string.Empty);
                }
            }

            return result;
        }
    }
复制代码

获取枚举描述

复制代码
//使用
string desc = CommonEnums.people.man.GetDescription();
//方法
public static string GetDescription(this Enum value, bool nameInstend = true)
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name == null)
            {
                return null;
            }
            FieldInfo field = type.GetField(name);
            DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
            if (attribute == null && nameInstend == true)
            {
                return name;
            }
            return attribute == null ? null : attribute.Description;
        }
复制代码

 

 

 

 

 

posted @   Doc.stu  阅读(291)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
点击右上角即可分享
微信分享提示