C# 枚举的获取方法

  1. 一、Enum的定义 
  2. public enum UserRolesType 
  3. {
  4.   UnKnown=0,
  5.   BaseSimple=70,
  6.   BaseBasic=71,
  7.   
  8.   BaseExtend=72,
  9.   BaseBasic2=88,
  10.   BaseSimple2=89,
  11.   BaseExtend2=90
  12. }
  13. 方法一:根据ID获取枚举对象 
  14.  protected UserRolesType GetEnum(int t)
  15. {
  16.     bool isInEnum = false;
  17.     UserRolesType c = UserRolesType.UnKnown;
  18.     if (t > 0)
  19.     { 
  20.          foreach(int i in Enum.GetValues(typeof(UserRolesType)))
  21.          {
  22.                if (i == t){//this.Debug(t.ToString(), "");
  23.                    c =(UserRolesType)Enum.Parse(typeof(UserRolesType),i.ToString());
  24.                    isInEnum = true;return c;
  25.                    //(Colors)Enum.Parse(typeof(Colors), "Red, Yellow");
  26.           }
  27.      }
  28. if (isInEnum == false)
  29. {
  30.      return UserRolesType.UnKnown;
  31. }
  32. return c; 
  33. }
  34. 方法二:根据ID获取枚举名称
  35. protected string GetEnumName(int s)
  36. {
  37. string str=Enum.GetName(typeof(UserRolesType), s);
  38. if (str == null)
  39. {
  40. str = UserRolesType.UnKnown.ToString();
  41. }
  42. return str;
  43. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值