枚举Enum

 #region 根据枚举名称获取值或反之
        /// <summary>
        /// 根据枚举的名称,得到该枚中该名称对应的值
        /// </summary>
        /// <param name="p_EnumVar">指定的枚举</param>
        /// <param name="p_Name">名称</param>
        /// <returns>值,如果没有,返回-1</returns>
        public static int GetEnumValueByName(Type p_EnumVar, string p_Name)
        {
            string[] names = Enum.GetNames(p_EnumVar);
            int nValue = -1;
            int[] nValues = (int[])Enum.GetValues(p_EnumVar);
            for (int i = names.Length - 1; i >= 0; i--)
            {
                //找到
                if (names[i] == p_Name)
                {
                    nValue = nValues[i];
                    break;
                }
            }
            return nValue;
        }

        /// <summary>
        /// 根据枚举的值,得到枚举变量的名称
        /// </summary>
        /// <param name="p_EnumVar">枚举</param>
        /// <param name="p_Value">值</param>
        /// <returns>变量名,如果没有找到,返回""</returns>
        public static string GetEnumNameByValue(Type p_EnumVar, int p_Value)
        {
            string name = "";
            string[] names = Enum.GetNames(p_EnumVar);
            int[] nValues = (int[])Enum.GetValues(p_EnumVar);
            for (int i = nValues.Length - 1; i >= 0; i--)
            {
                if (nValues[i] == p_Value)
                {
                    name = names[i];
                    break;
                }
            }
            return name;
        }
        #endregion

posted @ 2016-08-20 14:29  songyulan  Views(124)  Comments(0Edit  收藏  举报