zoukankan      html  css  js  c++  java
  • C# 枚举,传入int值返回string值

    需求:1:子公司负责人2:人事3:审批人4:签批人 5:管理员  传入值为1,2,3,4,5这个数字的某一个。需要返回他们的中文描述。

    一下忘记该怎么写了。。。后来百度下查出来了。。记录下当个小工具吧

    下面贴源码:

      //需要的方法
       public string GetEnumDescription(Enum enumValue)
            {
                string str = enumValue.ToString();
                System.Reflection.FieldInfo field = enumValue.GetType().GetField(str);
                object[] objs = field.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
                if (objs == null || objs.Length == 0) return str;
                System.ComponentModel.DescriptionAttribute da = (System.ComponentModel.DescriptionAttribute)objs[0];
                return da.Description;
            }
        //定义枚举
         enum RoleType
        {
            [Description("子公司负责人")]
            ZMSManager = 1,
            [Description("集团人力")]
            JTHR = 2,
            [Description("考核人")]
            AssessPerson = 3,
            [Description("订立人")]
            MakePerson = 4,
            [Description("系统管理员")]
            SysManager = 5
        }
      //调用方法
      string returnValue = GetEnumDescription((RoleType)(Enum.Parse(typeof(RoleType),"1"))); //返回值字符串:子公司负责人

    参考博客:http://www.cnblogs.com/xiaofengfeng/p/4125003.html

  • 相关阅读:
    Vijos1986
    vijos1790
    洛谷1005
    洛谷3381
    bzoj4034
    bzoj1564
    bzoj2648
    洛谷3348
    jzoi4964
    codevs3990
  • 原文地址:https://www.cnblogs.com/holyson/p/4952290.html
Copyright © 2011-2022 走看看