| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="EnumExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.ComponentModel; |
| | | 9 | | using System.Globalization; |
| | | 10 | | using MyNet.Humanizer.Display; |
| | | 11 | | using MyNet.Primitives; |
| | | 12 | | using MyNet.Reflection; |
| | | 13 | | |
| | | 14 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 15 | | namespace MyNet.Humanizer; |
| | | 16 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Contains extension methods for humanizing Enums. |
| | | 20 | | /// </summary> |
| | | 21 | | public static class EnumExtensions |
| | | 22 | | { |
| | | 23 | | extension(Enum value) |
| | | 24 | | { |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets the display name using an explicit provider resolved from DI. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="provider">The display name provider.</param> |
| | | 29 | | /// <param name="culture">The culture to use when retrieving the display name.</param> |
| | | 30 | | /// <returns>The display name of the enum value.</returns> |
| | | 31 | | public string HumanizeWith(IDisplayTextStrategy<Enum> provider, CultureInfo? culture = null) |
| | 0 | 32 | | => value.HumanizeWith(provider, DisplayTextOptions.Default, culture); |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets the display name using an explicit provider resolved from DI. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="provider">The display name provider.</param> |
| | | 38 | | /// <param name="options">The options to use when retrieving the display name.</param> |
| | | 39 | | /// <param name="culture">The culture to use when retrieving the display name.</param> |
| | | 40 | | /// <returns>The display name of the enum value.</returns> |
| | | 41 | | public string HumanizeWith(IDisplayTextStrategy<Enum> provider, DisplayTextOptions options, CultureInfo? culture |
| | | 42 | | { |
| | 0 | 43 | | ArgumentNullException.ThrowIfNull(provider); |
| | 0 | 44 | | return provider.GetDisplayText(value, options, culture.OrCurrent()); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets the description of the enum value, if it has a <see cref="DescriptionAttribute"/>. Otherwise, returns n |
| | | 49 | | /// </summary> |
| | | 50 | | /// <returns>The description of the enum value, or null if not defined.</returns> |
| | 6 | 51 | | public string? GetDescription() => value.GetAttribute<DescriptionAttribute>()?.Description; |
| | | 52 | | } |
| | | 53 | | } |
| | | 54 | | |