| | | 1 | | using System.ComponentModel; |
| | | 2 | | |
| | | 3 | | namespace ArturRios.Extensions; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides extension methods for enums, such as retrieving the DescriptionAttribute value. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class EnumExtensions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Gets the value of the <see cref="DescriptionAttribute" /> for the enum value, if present. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="value">The enum value.</param> |
| | | 14 | | /// <returns>The description text, or null if not found.</returns> |
| | | 15 | | public static string? GetDescription(this Enum value) |
| | 3 | 16 | | { |
| | 3 | 17 | | var field = value.GetType().GetField(value.ToString()); |
| | 3 | 18 | | var attribute = field?.GetCustomAttributes(typeof(DescriptionAttribute), false) |
| | 3 | 19 | | .Cast<DescriptionAttribute>() |
| | 3 | 20 | | .FirstOrDefault(); |
| | | 21 | | |
| | 3 | 22 | | return attribute?.Description; |
| | 3 | 23 | | } |
| | | 24 | | } |