如何从值中获取C#Enum描述?
public enum MyEnum{ Name1 = 1, [Description("Here is another")] HereIsAnother = 2, [Description("Last one")] LastOne = 3}
public static string GetEnumDescription(Enum value){ FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; if (attributes != null && attributes.Any()) { return attributes.First().Description; } return value.ToString();}
var myEnumDescriptions = from MyEnum n in Enum.GetValues(typeof(MyEnum)) select new { ID = (int)n, Name = Enumerations.GetEnumDescription(n) };