zoukankan      html  css  js  c++  java
  • C# 中 枚举Enum 一些转换的方法整理

     工作中 经常遇到枚举 的一些转换  特别是获取枚举备注等  特地整理下 方法以后使用

    public void TestMethod1()
            {
                TestEnumOne colorEnum = TestEnumOne.Red;
                int colorValue = 0x0000FF;
                string colorStr = "Red";
                string colorDes = "红色";
    
                //枚举To枚举字符串
                colorStr = colorEnum.ToString();
                colorStr = Enum.GetName(typeof(TestEnumOne), colorEnum);
                //枚举值To枚举字符串
                colorStr = Enum.GetName(typeof(TestEnumOne), colorValue);
    
                //枚举To枚举值
                colorValue = colorEnum.GetHashCode();
                colorValue = (int)colorEnum;
                //枚举字符To枚举值
                colorValue = Enum.Parse(typeof(TestEnumOne), colorStr).GetHashCode();
                colorValue = (int)Enum.Parse(typeof(TestEnumOne), colorStr);
    
                //枚举字To枚举
                colorEnum = (TestEnumOne)Enum.Parse(typeof(TestEnumOne), colorStr);
                //枚举值To枚举
                colorEnum = (TestEnumOne)colorValue;
    
                //根据枚举获取备注
                colorDes = TestEnumOne.Red.GetEnumDescriptionByEnum(typeof(TestEnumOne));
                //根据枚举值获取备注
                colorDes = TestEnumOne.Blue.GetEnumDescriptionByEnumValue(typeof(TestEnumOne), colorValue);
                //根据枚举字符串获取备注
                colorDes = TestEnumOne.Blue.GetEnumDescriptionByEnumString(typeof(TestEnumOne), colorStr);
            }
    

      下面是一个 枚举  和上面用到的一些方法

    public static class EnumClass
        {
            public enum TestEnumOne
            {
                [Description("红色")]
                Red = 0xff0000,
                [Description("橙色")]
                Orange = 0xFFA500,
                [Description("黄色")]
                Yellow = 0xFFFF00,
                [Description("蓝色")]
                Blue = 0x0000FF,
            }
    
            /// <summary>
            /// 根据枚举获取备注
            /// </summary>
            /// <param name="aEnum">枚举</param>
            /// <param name="enumType">枚举类型</param>
            /// <returns>备注</returns>
            public static string GetEnumDescriptionByEnum(this Enum aEnum, System.Type enumType)
            {
                string enumDescription = string.Empty;
                foreach (System.Enum enumItem in System.Enum.GetValues(enumType))
                {
                    string enumString = Enum.GetName(enumType, aEnum);
                    if (enumString.ToLower() == Enum.GetName(enumType, enumItem).ToLower())
                    {
                        FieldInfo fi = enumType.GetField(enumString);
                        DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, enumType);
                        if (da != null)
                        {
                            enumDescription = da.Description;
                        }
                    }
                }
                return enumDescription;
            }
    
            /// <summary>
            /// 根据枚举值获取备注
            /// </summary>
            /// <param name="aEnum">枚举</param>
            /// <param name="enumType">枚举类型</param>
            /// <param name="enumValue">枚举值</param>
            /// <returns>备注</returns>
            public static string GetEnumDescriptionByEnumValue(this Enum aEnum, System.Type enumType, int enumValue)
            {
                string enumDescription = string.Empty;
                foreach (System.Enum enumItem in System.Enum.GetValues(enumType))
                {
                    if (enumItem.GetHashCode() == (int)enumValue)
                    {
                        string enumString = Enum.GetName(enumType, enumValue);
                        FieldInfo fi = enumType.GetField(enumString);
                        DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, enumType);
                        if (da != null)
                        {
                            enumDescription = da.Description;
                        }
                    }
                }
                return enumDescription;
            }
    
            /// <summary>
            /// 根据枚举字符串获取备注
            /// </summary>
            /// <param name="aEnum">枚举</param>
            /// <param name="enumType">枚举类型</param>
            /// <param name="enumValue">枚举值</param>
            /// <returns>备注</returns>
            public static string GetEnumDescriptionByEnumString(this Enum aEnum, System.Type enumType, string enumString)
            {
                string enumDescription = string.Empty;
                foreach (System.Enum enumItem in System.Enum.GetValues(enumType))
                {
                    if (enumString.ToLower() == Enum.GetName(enumType, enumItem).ToLower())
                    {
                        FieldInfo fi = enumType.GetField(enumString);
                        DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, enumType);
                        if (da != null)
                        {
                            enumDescription = da.Description;
                        }
                    }
                }
                return enumDescription;
            }
        }
  • 相关阅读:
    Mysql登录错误:ERROR 1045 (28000): Plugin caching_sha2_password could not be loaded
    Docker配置LNMP环境
    Docker安装mysqli扩展和gd扩展
    Docker常用命令
    Ubuntu常用命令
    单例模式的优缺点和使用场景
    ABP 多租户数据共享
    ABP Core 后台Angular+Ng-Zorro 图片上传
    ERROR Error: If ngModel is used within a form tag, either the name attribute must be set or the form control must be defined as 'standalone' in ngModelOptions.
    AbpCore 执行迁移文件生成数据库报错 Could not find root folder of the web project!
  • 原文地址:https://www.cnblogs.com/burg-xun/p/7091040.html
Copyright © 2011-2022 走看看