您当前位置:编程帮手 > 知识 > 知识 > C# > 枚举 > 内容
代码库
2014-07-13 10:30:54 访问(3795) 赞(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);
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);
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() + "没有找到对应的枚举值。"); }
上一条:
下一条: