forked from simpleway2016/JMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigurationValue.cs
47 lines (44 loc) · 1.32 KB
/
ConfigurationValue.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
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;
namespace JMS.Common
{
public class ConfigurationValue<T>
{
public event EventHandler<ValueChangedArg<T>> ValueChanged;
/// <summary>
/// 转换后对象,对象会自动随着配置文件更新而更新
/// </summary>
public T Current { get; private set; }
IDisposable _disposable;
internal ConfigurationValue(IConfiguration configuration)
{
callback(configuration);
}
void callback(object state)
{
_disposable?.Dispose();
IConfiguration configuration = (IConfiguration)state;
var old = this.Current;
this.Current = configuration.Get<T>();
_disposable = configuration.GetReloadToken().RegisterChangeCallback(callback, configuration);
try
{
this.ValueChanged?.Invoke(this, new ValueChangedArg<T>
{
OldValue = old,
NewValue = this.Current
});
}
catch
{
}
}
}
public class ValueChangedArg<T>
{
public T OldValue { get; set; }
public T NewValue { get; set; }
}
}