Add configurations and update packages

This commit is contained in:
2021-07-15 22:28:26 +02:00
parent 84b5e44a83
commit c84077ef58
23 changed files with 451 additions and 102 deletions

View File

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