c#获取枚举所有项列表包含Description描述


 public enum UserType
    {
        /// 
        /// 后台管理
        /// 
        [Description("后台管理")]
        Manager = 1,
        /// 
        /// 普通用户
        /// 
        [Description("普通用户")]
        Viewer = 2,
    }


/// 
/// 获取枚举类型的所有项,返回集合
/// 
/// 枚举类型
/// 返回System.ValueTuple集合类型,item1为描述名称,item2为枚举name,item3为枚举int值
public static List<(string, string, int)> GetEnumList(Type enumType)
{
	List<(string, string, int)> list = new List<(string, string, int)>();
	//var arr2 = typeof(Operation).GetFields();
	var arr2 = enumType.GetFields();
	for (int i = 1; i < arr2.Length; i++)
	{
		var item = arr2[i];
		System.ComponentModel.DescriptionAttribute[] dd = item.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true) as System.ComponentModel.DescriptionAttribute[];
		string des = dd[0].Description;
		var name = item.Name;
		//int num = (int)Enum.Parse(typeof(Operation), name);
		int num = (int)Enum.Parse(enumType, name);
		list.Add((des, name, num));
	}
	return list;
}

 

你可能感兴趣的:(C#,枚举获取所欲项)