SettingsStorage
Provides a synchronized storage for application settings with support for XML serialization.
Inherits: SynchronizedDictionary<string, object>
Constructors
public SettingsStorage()
settingsStorage = SettingsStorage()
Initializes a new instance of the SettingsStorage class.
internal SettingsStorage(JsonReader reader, Func<JsonReader, SettingsStorage, string, Type, CancellationToken, ValueTask<object>> readJson, CancellationToken cancellationToken)
settingsStorage = SettingsStorage(reader, readJson, cancellationToken)
Initializes a new instance of the SettingsStorage class using the specified JSON reader and delegate for reading JSON.
- reader
- The JSON reader used for deserialization.
- readJson
- The delegate to read JSON values.
- cancellationToken
- The cancellation token captured for the synchronous reader-mode value reads.
Properties
internal int DeepLevel { get; set; }
value = settingsStorage.DeepLevel
settingsStorage.DeepLevel = value
Gets or sets the deep level of deserialization.
public IEnumerable<string> Names { get; }
value = settingsStorage.Names
Gets the names of all stored settings.
Methods
public bool Contains(string name)
result = settingsStorage.Contains(name)
Determines whether the storage contains a setting with the specified name.
- name
- The name of the setting.
Returns: true if the setting exists; otherwise, false.
public override bool ContainsKey(string key)
result = settingsStorage.ContainsKey(key)
Determines whether the storage contains a setting with the given key.
- key
- The key of the setting.
Returns: true if the key exists; otherwise, false.
private void EnsureIsNotReader()
settingsStorage.EnsureIsNotReader()
Ensures that the current instance is not used as a reader.
public object GetValue(Type type, string name, object defaultValue)
result = settingsStorage.GetValue(type, name, defaultValue)
Gets the value of a setting with the specified name and converts it to the given type.
- type
- The type of value expected.
- name
- The name of the setting.
- defaultValue
- The default value if the setting is not found or null.
Returns: The setting value converted to the specified type.
public ValueTask<object> GetValueAsync(Type type, string name, object defaultValue, CancellationToken cancellationToken)
result = settingsStorage.GetValueAsync(type, name, defaultValue, cancellationToken)
Asynchronously gets the value of a setting with the specified name and converts it to the given type.
- type
- The expected type of the value.
- name
- The name of the setting.
- defaultValue
- The default value if the setting is not found.
- cancellationToken
- The cancellation token.
Returns: A ValueTask representing the asynchronous operation producing the setting value converted to the specified type.
public ValueTask<T> GetValueAsync<T>(string name, T defaultValue, CancellationToken cancellationToken)
result = settingsStorage.GetValueAsync(name, defaultValue, cancellationToken)
Asynchronously gets the value of a setting with the specified name and converts it to the specified type.
- name
- The name of the setting.
- defaultValue
- The default value if the setting is not found.
- cancellationToken
- The cancellation token.
Returns: A ValueTask representing the asynchronous operation producing the value converted to type .
private ValueTask<object> GetValueFromReaderAsync(Type type, string name, CancellationToken cancellationToken)
result = settingsStorage.GetValueFromReaderAsync(type, name, cancellationToken)
Asynchronously gets the value from the JSON reader using the provided delegate.
- type
- The expected type of the value.
- name
- The name of the setting.
- cancellationToken
- The cancellation token.
Returns: A ValueTask representing the asynchronous operation producing the value converted to the specified type.
public T GetValue<T>(string name, T defaultValue)
result = settingsStorage.GetValue(name, defaultValue)
Gets the value of a setting with the specified name and converts it to the specified type.
- name
- The name of the setting.
- defaultValue
- The default value if the setting is not found or is null.
Returns: The setting value converted to type .
public void SetValue<T>(string name, T value)
settingsStorage.SetValue(name, value)
Sets the value for a specified setting name.
- name
- The name of the setting.
- value
- The value to set.
public SettingsStorage Set<T>(string name, T value)
result = settingsStorage.Set(name, value)
Sets the value for a specified setting name.
- name
- The name of the setting.
- value
- The value to set.
Returns: The current instance of SettingsStorage.
public object TryGet(Type type, string name, object defaultValue)
result = settingsStorage.TryGet(type, name, defaultValue)
Gets the value of a setting with the specified name and converts it to the given type. Returns a default value if not found.
- type
- The expected type of the setting.
- name
- The name of the setting.
- defaultValue
- The default value if the setting is not found.
Returns: The setting value converted to the specified type.
public T TryGet<T>(string name, T defaultValue)
result = settingsStorage.TryGet(name, defaultValue)
Gets the value of a setting with the specified name and converts it to the specified type. Returns a default value if not found.
- name
- The name of the setting.
- defaultValue
- The default value if the setting is not found.
Returns: The setting value converted to type .