feat: added default valueless Get to IConfiguration for nullables

This commit is contained in:
2026-03-31 17:21:18 +03:00
parent fe0173b091
commit c11aced8d2
2 changed files with 6 additions and 4 deletions

View File

@@ -12,17 +12,18 @@ public class BasicConfiguration : IConfiguration
public IReadOnlyDictionary<string, object?> Values => values; public IReadOnlyDictionary<string, object?> Values => values;
public T? Get<T>(string key, T? defaultValue = default) public T Get<T>(string key, T defaultValue) => Get<T>(key) ?? defaultValue;
public T? Get<T>(string key)
{ {
if (!values.TryGetValue(key, out object? value)) if (!values.TryGetValue(key, out object? value))
return defaultValue; return default;
if (value is T castedObject) if (value is T castedObject)
return castedObject; return castedObject;
try { return (T?)System.Convert.ChangeType(value, typeof(T)); } catch { } try { return (T?)System.Convert.ChangeType(value, typeof(T)); } catch { }
return defaultValue; return default;
} }
public object? Get(string key) public object? Get(string key)

View File

@@ -15,7 +15,8 @@ public interface IConfiguration
bool Has(string key); bool Has(string key);
object? Get(string key); object? Get(string key);
T? Get<T>(string key, T? defaultValue = default); T Get<T>(string key, T defaultValue);
T? Get<T>(string key);
void Set<T>(string key, T value); void Set<T>(string key, T value);
void Remove<T>(string key); void Remove<T>(string key);