28 lines
997 B
C#
28 lines
997 B
C#
using Configuration.Core;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace Core.Configurations.ConfigurationManager
|
|
{
|
|
public static class ConfigurationExtensions
|
|
{
|
|
public static T Get<T>(this IConfigurationSection section) where T : class
|
|
{
|
|
if (section is JsonConfigurationSection jsonSection)
|
|
{
|
|
var token = jsonSection.GetToken();
|
|
return token?.ToObject<T>();
|
|
}
|
|
throw new InvalidOperationException("Section is not a JsonConfigurationSection");
|
|
}
|
|
|
|
public static T GetValue<T>(this IConfigurationSection section, string key)
|
|
{
|
|
if (section is JsonConfigurationSection jsonSection)
|
|
{
|
|
var token = jsonSection.GetToken().SelectToken(key.Replace(":", "."));
|
|
return token.ToObject<T>();
|
|
}
|
|
throw new InvalidOperationException("Section is not a JsonConfigurationSection");
|
|
}
|
|
}
|
|
}
|