首页 > 代码库 > C#枚举类型的常用操作总结

C#枚举类型的常用操作总结

  枚举类型是定义了一组“符号名称/值”配对。枚举类型是强类型的。每个枚举类型都是从system.Enum派生,又从system.ValueType派生,而system.ValueType又从system.Object派生,所以枚举类型是指类型。

  编译枚举类型时,C#编译器会把每个符号转换成类型的一个常量字段。C#编译器将枚举类型视为基元类型。

 1.获取枚举列表:

        /// <summary>
        /// 获取枚举列表
        /// </summary>
        /// <param name="enumType">枚举的类型</param>
        /// <returns>枚举列表</returns>
        public static Dictionary<int, string> GetEnumList(Type enumType)
        {
            var dic = new Dictionary<int, string>();
            try
            {
                var fd = enumType.GetFields();
                for (var index = 1; index < fd.Length; ++index)
                {
                    var info = fd[index];
                    var fieldValue = System.Enum.Parse(enumType, fd[index].Name);
                    var attrs = info.GetCustomAttributes(typeof(EnumTextAttribute), false);
                    foreach (EnumTextAttribute attr in attrs)
                    {
                        var key = (int)fieldValue;
                        if (key == -100) continue;
                        var value = attr.Text;
                        dic.Add(key, value);
                    }
                }
                return dic;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

2.获取枚举名称:

        /// <summary>
        /// 获取枚举名称
        /// </summary>
        /// <param name="enumType">枚举的类型</param>
        /// <param name="id">枚举值</param>
        /// <returns>如果枚举值存在,返回对应的枚举名称,否则,返回空字符</returns>
        public static string GetEnumTextById(Type enumType, int id)
        {
            var ret = string.Empty;
            try
            {
                var dic = GetEnumList(enumType);
                foreach (var item in dic)
                {
                    if (item.Key != id) continue;
                    ret = item.Value;
                    break;
                }
                return ret;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

3.根据枚举值获取对应中文描述:

        /// <summary>
        /// 根据枚举值获取对应中文描述
        /// </summary>
        /// <param name="enumValue">枚举值</param>
        /// <returns>枚举值中文描述</returns>
        public static string GetEnumTextByEnum(object enumValue)
        {
            var ret = string.Empty;
            if ((int)enumValue == -1) return ret;
            try
            {
                var dic = GetEnumList(enumValue.GetType());
                foreach (var item in dic)
                {
                    if (item.Key != (int)enumValue) continue;
                    ret = item.Value;
                    break;
                }
                return ret;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

4.获取枚举名称:

        /// <summary>
        /// 获取枚举名称
        /// </summary>
        /// <param name="enumType">枚举的类型</param>
        /// <param name="index">枚举值的位置编号</param>
        /// <returns>如果枚举值存在,返回对应的枚举名称,否则,返回空字符</returns>
        public static string GetEnumTextByIndex(Type enumType, int index)
        {
            var ret = string.Empty;
            var dic = GetEnumList(enumType);
            if (index < 0 || index > dic.Count)
                return ret;
            var i = 0;
            foreach (var item in dic)
            {
                if (i == index)
                {
                    ret = item.Value;
                    break;
                }
                i++;
            }

            return ret;
        }

5.获取枚举值:

        /// <summary>
        /// 获取枚举值
        /// </summary>
        /// <param name="enumType">枚举的类型</param>
        /// <param name="name">枚举名称</param>
        /// <returns>如果枚举名称存在,返回对应的枚举值,否则,返回-1</returns>
        public static int GetEnumIdByName(Type enumType, string name)
        {
            var ret = -1;
            if (string.IsNullOrEmpty(name))
                return ret;
            var dic = GetEnumList(enumType);
            foreach (var item in dic)
            {
                if (string.Compare(item.Value, name, StringComparison.Ordinal) != 0) continue;
                ret = item.Key;
                break;
            }
            return ret;
        }

6.获取名字对应枚举值:

        /// <summary>
        /// 获取名字对应枚举值
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="name">枚举名称</param>
        /// <returns></returns>
        public static T GetEnumIdByName<T>(string name) where T : new()
        {
            var type = typeof(T);

            var enumItem = (T)TypeDescriptor.GetConverter(type).ConvertFrom("-1");
            if (string.IsNullOrEmpty(name))
                return enumItem;

            try
            {
                var fd = typeof(T).GetFields();
                for (var index = 1; index < fd.Length; ++index)
                {
                    var info = fd[index];
                    var fieldValue = System.Enum.Parse(type, fd[index].Name);
                    var attrs = info.GetCustomAttributes(typeof(EnumTextAttribute), false);
                    if (attrs.Length != 1) continue;
                    var attr = (EnumTextAttribute)attrs[0];
                    if (!name.Equals(attr.Text)) continue;
                    enumItem = (T)fieldValue;
                    break;
                }
                return enumItem;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

7.获取枚举值所在的位置编号:

        /// <summary>
        /// 获取枚举值所在的位置编号
        /// </summary>
        /// <param name="enumType">枚举的类型</param>
        /// <param name="name">枚举名称</param>
        /// <returns>如果枚举名称存在,返回对应的枚举值的位置编号,否则,返回-1</returns>
        public static int GetEnumIndexByName(Type enumType, string name)
        {
            var ret = -1;

            if (string.IsNullOrEmpty(name))
                return ret;

            var dic = GetEnumList(enumType);
            var i = 0;
            foreach (var item in dic)
            {
                if (string.Compare(item.Value, name, StringComparison.Ordinal) == 0)
                {
                    ret = i;
                    break;
                }
                i++;
            }

            return ret;
        }


本文出自 “彭泽0902” 博客,请务必保留此出处http://pengze0902.blog.51cto.com/7693836/1862711

C#枚举类型的常用操作总结