-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cs
More file actions
executable file
·52 lines (42 loc) · 1.39 KB
/
example.cs
File metadata and controls
executable file
·52 lines (42 loc) · 1.39 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env dotnet
#:package Configuration.Writable@*
using System.Text.Json.Serialization;
using Configuration.Writable;
using Configuration.Writable.FormatProvider;
// initialize
WritableOptions.Initialize<SampleSetting>(conf =>
{
conf.UseFile("usersettings.json");
conf.FormatProvider = new JsonAotFormatProvider(SampleSettingSerializerContext.Default);
});
// get the writable options instance
var options = WritableOptions.GetOptions<SampleSetting>();
// get values
Console.WriteLine($"Current Name: {options.CurrentValue.Name}");
// optionally, you can register change callback
options.OnChange(newSetting =>
{
Console.WriteLine($">> Settings changed! Name: {newSetting.Name}");
});
// and save to storage
Console.Write("Enter new name: ");
var newName = Console.ReadLine() ?? "";
await options.SaveAsync(setting =>
{
setting.Name = newName;
});
// announce saved location
var savedLocation = options.GetOptionsConfiguration().ConfigFilePath;
Console.WriteLine($"Saved to {savedLocation}");
// need some delay to see the change callback in action
await Task.Delay(100);
// ------
// setting class
[OptionsModel]
public partial class SampleSetting
{
public string Name { get; set; } = "default name";
}
// source generation context for System.Text.Json serialization
[JsonSerializable(typeof(SampleSetting))]
public partial class SampleSettingSerializerContext : JsonSerializerContext;