字符串值转成枚举

2014-07-13 10:30:54  访问(3824) 赞(0) 踩(0)

传统方法通过值转成枚举

        string themeValue = "1"; // "1" 为管理员对应的枚举值 //

        SlowX.WebKnowledge.Enums.UserRole.EmUserRole curEm = (SlowX.WebKnowledge.Enums.UserRole.EmUserRole)Enum.Parse(typeof(SlowX.WebKnowledge.Enums.UserRole.EmUserRole), themeValue, true);


传统方法通过枚举的Text转成枚举


        string themeValue = "部门领导";

        SlowX.WebKnowledge.Enums.UserRole.EmUserRole curEm = (SlowX.WebKnowledge.Enums.UserRole.EmUserRole)Enum.Parse(typeof(SlowX.WebKnowledge.Enums.UserRole.EmUserRole), themeValue, true);


框架方法通过值转成枚举


        string enumValue = "1"; // "1" 为管理员对应的枚举值 //

        SlowX.WebKnowledge.Enums.UserRole.EmUserRole curEm =
            SlowX.WebKnowledge.Enums.UserRole.GetEmByString(enumValue);

框架方法通过枚举Text转成枚举


        string enumValue = "部门领导";  

        SlowX.WebKnowledge.Enums.UserRole.EmUserRole curEm =
            SlowX.WebKnowledge.Enums.UserRole.GetEmByText(enumValue);

框架方法相关内部代码段


        /// <summary>
        /// 数组
        /// </summary>
        public readonly static EmUserRole[] EmArray = new EmUserRole[]
        {
            EmUserRole.管理员,
            EmUserRole.总经理,
            EmUserRole.部门领导,
            EmUserRole.普通职员
        };


        /// <summary>
        /// 获得枚举
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        public static EmUserRole GetEmByString(string theValue)
        {
            EmUserRole[] emA = EmArray;

            foreach (EmUserRole em in emA)
            {
                if (theValue == ((int)em).ToString())
                    return em;
            }

            MethodBase methodBaseValue = MethodBase.GetCurrentMethod();

            throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> " + "theValue = " + theValue.ToString() + "没有找到对应的枚举值。");

        }

        /// <summary>
        /// 获得 Text 获得 EmUserRole 
        /// </summary>
        /// <param name="theText"></param>
        /// <returns></returns>
        public static EmUserRole GetEmByText(string theText)
        {
            MethodBase methodBaseValue = null;

            if (theText == null)
            {
                methodBaseValue = MethodBase.GetCurrentMethod();

                throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> " + "theText为null,没有找到对应的枚举值。");
            }

            if (theText.Length == 0)
            {
                methodBaseValue = MethodBase.GetCurrentMethod();

                throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> " + "theText为空,没有找到对应的枚举值。");
            }

            EmUserRole[] emA = EmArray;

            foreach (EmUserRole em in emA)
            {
                if (theText == (em).ToString())
                    return em;
            }

            methodBaseValue = MethodBase.GetCurrentMethod();

            throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> " + "theText = " + theText.ToString() + "没有找到对应的枚举值。");
        }



标签:字符串值转成枚举    String    获得枚举    转成枚举    代码示例 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)