Use file scoped namepace

This commit is contained in:
2022-12-22 15:29:53 +01:00
parent 27bf5f0b24
commit 6d7e447ccc
51 changed files with 886 additions and 937 deletions

View File

@@ -2,28 +2,27 @@
using System.Linq;
using Microsoft.Extensions.Configuration;
namespace GerstITS.Examples.Api.Configurations
namespace GerstITS.Examples.Api.Configurations;
public static class IConfigurationExtensions
{
public static class IConfigurationExtensions
#region Methods
public static TEnum GetFlaggedEnum<TEnum>(this IConfiguration configuration, string key)
where TEnum : struct, IConvertible
{
#region Methods
ThrowsAnExceptionIfTypeIsNotAnEnumeration<TEnum>();
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
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
}