Advertisement
AgentFire

Untitled

Mar 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. internal static class EnumHelper
  2. {
  3. public static IReadOnlyDictionary<TEnum, string> WithDescriptions<TEnum>() where TEnum : struct
  4. {
  5. if (!typeof(TEnum).IsEnum)
  6. {
  7. throw new ArgumentException("Must be enum", nameof(TEnum));
  8. }
  9.  
  10. var query = from f in typeof(TEnum).GetFields(BindingFlags.Public | BindingFlags.Static)
  11. let attr = f.GetCustomAttribute<DescriptionAttribute>()
  12. let value = (TEnum)f.GetValue(null)
  13. select (value, attr.Description);
  14.  
  15. return query.ToDictionary(T => T.value, T => T.Description);
  16. }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement