feat: added default valueless Get to IConfiguration for nullables
This commit is contained in:
@@ -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)
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user