mirror of
https://github.com/AlexMacocian/SystemExtensions.git
synced 2026-07-15 14:19:29 +00:00
e6a06915cd
Codestyle fixes Setup CODEOWNERS Setup dependabot
33 lines
1.1 KiB
C#
33 lines
1.1 KiB
C#
using Slim.Resolvers;
|
|
using System.Extensions.Configuration;
|
|
|
|
namespace System.Configuration;
|
|
|
|
public sealed class UpdateableOptionsResolver : IDependencyResolver
|
|
{
|
|
private static readonly Type optionsType = typeof(UpdateableOptionsWrapper<>);
|
|
private static readonly Type configurationType = typeof(IOptionsManager);
|
|
|
|
public bool CanResolve(Type type)
|
|
{
|
|
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IUpdateableOptions<>))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public object Resolve(Slim.IServiceProvider serviceProvider, Type type)
|
|
{
|
|
var typedOptionsType = optionsType.MakeGenericType(type.GetGenericArguments());
|
|
var configurationManager = serviceProvider.GetService<IOptionsManager>();
|
|
var optionsValue = configurationType
|
|
.GetMethod(nameof(IOptionsManager.GetOptions))
|
|
.MakeGenericMethod(type.GetGenericArguments())
|
|
.Invoke(configurationManager, Array.Empty<object>());
|
|
|
|
return Activator.CreateInstance(typedOptionsType, configurationManager, optionsValue);
|
|
}
|
|
}
|