C#遍历获取枚举的值,名和属性

时间:2022-02-20 03:15:49

获取:

    Type type = typeof(ParamServiceType);
var values = Enum.GetValues(type);
for (int i = ; i < values.Length; i++)
{
var v = values.GetValue(i);
var member = type.GetMember(v.ToString());
DescriptionAttribute des = (DescriptionAttribute)System.Attribute.GetCustomAttribute(member[], typeof(DescriptionAttribute));
//以下代码是业务需求
serviceRoot.children.Add(new ServiceTree()
{
id = (int)v,//取int值
text = des.Description,//取属性值
state = "open",
attributes = v.ToString(),//取名
children = null
});
}