using System; using System.Linq; using Microsoft.Extensions.Configuration; namespace GerstITS.Examples.Api.Configurations; public static class IConfigurationExtensions { #region Methods public static TEnum GetFlaggedEnum(this IConfiguration configuration, string key) where TEnum : struct, IConvertible { ThrowsAnExceptionIfTypeIsNotAnEnumeration(); return (TEnum)(object)(configuration.GetSection(key)? .Get() ?? Enumerable.Empty()) .Sum(i => (int)(object)i); } private static void ThrowsAnExceptionIfTypeIsNotAnEnumeration() where TEnum : struct, IConvertible { if (!typeof(TEnum).IsEnum) throw new InvalidOperationException($"{typeof(TEnum)} is not supported."); } #endregion }