forked from Particular/ServiceControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppConfigConfigurationSource.cs
More file actions
32 lines (26 loc) · 1.27 KB
/
AppConfigConfigurationSource.cs
File metadata and controls
32 lines (26 loc) · 1.27 KB
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
#nullable enable
namespace ServiceControl.Configuration;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
public class AppConfigConfigurationSource : IConfigurationSource
{
public IConfigurationProvider Build(IConfigurationBuilder builder)
{
var propertiesWithAttribute = from a in AppDomain.CurrentDomain.GetAssemblies()
from t in a.GetTypes()
from p in t.GetProperties()
let attributes = p.GetCustomAttributes(typeof(AppConfigSettingAttribute), true)
where attributes != null && attributes.Length > 0
select new { Type = p, Attribute = attributes.Cast<AppConfigSettingAttribute>().Single() };
var mappings = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
foreach (var property in propertiesWithAttribute)
{
var section = property.Type.DeclaringType!.Name.Replace("Options", "");
var name = property.Type.Name;
mappings[$"{section}:{name}"] = property.Attribute.Keys;
}
return new AppConfigConfigurationProvider(mappings);
}
}