Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal static class EnumHelper
- {
- public static IReadOnlyDictionary<TEnum, string> WithDescriptions<TEnum>() where TEnum : struct
- {
- if (!typeof(TEnum).IsEnum)
- {
- throw new ArgumentException("Must be enum", nameof(TEnum));
- }
- var query = from f in typeof(TEnum).GetFields(BindingFlags.Public | BindingFlags.Static)
- let attr = f.GetCustomAttribute<DescriptionAttribute>()
- let value = (TEnum)f.GetValue(null)
- select (value, attr.Description);
- return query.ToDictionary(T => T.value, T => T.Description);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement