C#获取枚举值的DescriptionAttribute内容
Apr 05 2018
1 2 3 4 5 6 7 8 9 10 11 12
| public static string GetDescription<T>(this T source) { FieldInfo fi = source.GetType().GetField(source.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes( typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0) return attributes[0].Description; else return source.ToString(); }
|
1 2 3
|
string description = ((MyEnum)value).AsString(EnumFormat.Description);
|