| | 1 | | using System.ComponentModel; |
| | 2 | |
|
| | 3 | | namespace Songhay.Extensions; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// Extensions of <see cref="Enum"/>. |
| | 7 | | /// </summary> |
| | 8 | | public static class EnumExtensions |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Gets any conventional <see cref="DescriptionAttribute.Description"/> |
| | 12 | | /// applied to an <see cref="Enum"/>. |
| | 13 | | /// </summary> |
| | 14 | | /// <param name="value">The <see cref="Enum"/>.</param> |
| | 15 | | public static string? GetEnumDescription(this Enum value) |
| 1 | 16 | | { |
| 1 | 17 | | var enumType = value.GetType(); |
| 1 | 18 | | var enumName = Enum.GetName(enumType, value); |
| 1 | 19 | | if (enumName == null) return null; |
| | 20 | |
|
| 1 | 21 | | FieldInfo? field = enumType.GetField(enumName); |
| 1 | 22 | | if (field == null) return null; |
| | 23 | |
|
| 1 | 24 | | var attr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; |
| | 25 | |
|
| 1 | 26 | | return attr?.Description; |
| 1 | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Gets the result of <see cref="Enum.GetValues"/> |
| | 31 | | /// based on the specified <see cref="Enum"/>. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="value">The <see cref="Enum"/>.</param> |
| | 34 | | public static IEnumerable<Enum> GetEnumValues(this Enum value) |
| 1 | 35 | | { |
| 1 | 36 | | var enumType = value.GetType(); |
| 1 | 37 | | var enums = Enum.GetValues(enumType).OfType<Enum>(); |
| | 38 | |
|
| 1 | 39 | | return enums; |
| 1 | 40 | | } |
| | 41 | | } |