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

  • 相关阅读:
    DC中为什么要用Uniquify?
    hdu 1596 find the safest road
    hdu2112 HDU Today
    hdu 2066 一个人的旅行
    poj 3026 Borg Maze
    poj 1979 Red and Black
    poj 1321 棋盘问题
    hdu 1010 Tempter of the Bone
    hdu 4861 Couple doubi
    codeforces584B Kolya and Tanya
  • 原文地址:https://www.cnblogs.com/holyson/p/4952290.html
Copyright © 2011-2022 走看看