forked from piechpatrick/SeaweedFs.Net
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExtensions.cs
47 lines (46 loc) · 1.83 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// ***********************************************************************
// Assembly : SeaweedFs
// Author : piechpatrick
// Created : 10-10-2021
//
// Last Modified By : piechpatrick
// Last Modified On : 10-10-2021
// ***********************************************************************
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace SeaweedFs
{
/// <summary>
/// Class Extensions.
/// </summary>
public static class Extensions
{
/// <summary>
/// Gets the options.
/// </summary>
/// <typeparam name="TModel">The type of the t model.</typeparam>
/// <param name="serviceCollection">The service collection.</param>
/// <param name="sectionName">Name of the section.</param>
/// <returns>TModel.</returns>
public static TModel GetOptions<TModel>(this IServiceCollection serviceCollection, string sectionName) where TModel : class, new()
{
using var serviceProvider = serviceCollection.BuildServiceProvider();
var configuration = serviceProvider.GetService<IConfiguration>();
return configuration.GetOptions<TModel>(sectionName);
}
/// <summary>
/// Gets the options.
/// </summary>
/// <typeparam name="TModel">The type of the t model.</typeparam>
/// <param name="configuration">The configuration.</param>
/// <param name="sectionName">Name of the section.</param>
/// <returns>TModel.</returns>
public static TModel GetOptions<TModel>(this IConfiguration configuration, string sectionName)
where TModel : new()
{
var model = new TModel();
configuration.GetSection(sectionName).Bind(model);
return model;
}
}
}