您的位置:首页 > 编程语言 > C#

C# 枚举,传入int值返回string值

2015-11-10 11:11 495 查看
需求: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: