C# 声明枚举、枚举值描述 获取枚举值字符串 根据枚举获取枚举值描述

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_23009105/article/details/94178618

一、声明枚举值、枚举值描述

using System.ComponentModel;

public enum status
{
	/// <summary>
	/// 取消
	/// </summary>
	[Description("取消")]
	cancel,
	/// <summary>
	/// 提交
	/// </summary>
	[Description("提交")]
	submit,
	/// <summary>
	/// 同意
	/// </summary>
	[Description("批准")]
	pass,
	/// <summary>
	/// 驳回
	/// </summary>
	[Description("驳回")]
	reject
}

二、获取枚举值字符串、根据枚举获取枚举值描述

/// <summary>
/// 获取枚举描述信息
/// </summary>
/// <param name="enumValue"></param>
/// <returns></returns>
public static string GetEnumDescription(this 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;
}

/*
示例:string statusDes = status.submit.GetEnumDescription();
*/

/// <summary>
/// 枚举字符串
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumValue"></param>
/// <returns></returns>
public static string GetEnumString<T>(this Enum enumValue) {
	return Enum.GetName(typeof(T) , enumValue);//推荐  
}

/*
示例:string status = status.submit.GetEnumString<status>();
*/

/// <summary>
/// 根据Description获取枚举
/// </summary>
/// <typeparam name="T">枚举类型</typeparam>
/// <param name="description">枚举描述</param>
/// <returns>枚举</returns>
public static string GetEnumName<T>(string description)
{
	Type _type = typeof(T);
	foreach (FieldInfo field in _type.GetFields())
	{
		DescriptionAttribute[] _curDesc = field.GetDescriptAttr();
		if (_curDesc != null && _curDesc.Length > 0)
		{
			if (_curDesc[0].Description == description)
				return field.GetValue(null).ToString();
		}
	}

	return null;
}

/*
示例:string status = GetEnumName<status>("提交");
*/

三、根据枚举值变量获取枚举描述信息

status statusEnum;
//方式1
Enum.TryParse<status>(strStatus, out statusEnum);

//方式2
statusEnum = (status)Enum.Parse(typeof(status), strStatus);

string strStatusDes = statusEnum.GetEnumDescription();

 

展开阅读全文

如何获取结构体中枚举枚举

05-21

[code=C#]rnenum studentsSex//性别rn rn 女,rn 男rn rnrn enum course : byte//科目rn rn 语文,rn 数学,rn 英语rn rnrn struct students//学生信息的结构体rn rn public studentsSex studentSex;rn public string studentName;rn public byte studentAge;rn public ushort studentStature;rn public course studentCourse;rn rn static void Main(string[] args)rn rnrn string path = Directory.GetCurrentDirectory();rn ushort studentNo;rn students[] student;rn EditorMessage(out student, out studentNo); rn rnrn private static void EditorMessage(out students[] student, out ushort studentNo)rn rn rn Console.Write("请输入您要录入的学生数量:");rn studentNo = Convert.ToUInt16(Console.ReadLine());rn student = new students[studentNo];rn for (uint i = 0; i < student.Length; i++)rn rn Console.WriteLine("请输入第0个学生的信息:", i + 1);rn Console.Write("姓名:");rn student[i].studentName = Console.ReadLine();rn Console.Write("年龄:");rn student[i].studentAge = Convert.ToByte(Console.ReadLine());rn Console.Write("性别:0)女1)男");rn student[i].studentSex = (studentsSex)Convert.ToByte(Console.ReadLine());rn Console.Write("身高(cm):");rn student[i].studentStature = Convert.ToUInt16(Console.ReadLine());rn Console.WriteLine("语文:");rn byte chinese = Convert.ToByte(Console.ReadLine());rn course myCourse = course.语文;//添加语文成绩rn myCourse = (course)chinese;rn student[i].studentCourse = myCourse;rn rn //录入完毕,打印出来。rn for (uint i = 0; i < student.Length; i++)rn rn Console.WriteLine("第0个学生信息:", i + 1);rn Console.WriteLine("姓名:0", student[i].studentName);rn Console.WriteLine("年龄:0", student[i].studentAge);rn Console.WriteLine("性别:0", student[i].studentSex);rn Console.WriteLine("身高:0", student[i].studentStature);rn Console.WriteLine("语文:0"); rn rn rn[/code]rnrnConsole.WriteLine("语文:0"); rn最后一步,怎么获取student[]里的course这个枚举的中“语文”对应的枚举值? 论坛

没有更多推荐了,返回首页