diff --git a/JackCraft.Config/Config.cs b/JackCraft.Config/Config.cs index f633fbd..ec5e5cc 100644 --- a/JackCraft.Config/Config.cs +++ b/JackCraft.Config/Config.cs @@ -41,17 +41,23 @@ public class Config public T? Get(string key, T? defaultValue) { + LoadConfigData(); var keys = key.Split('.'); var currentData = _configData; for (var i = 0; i < keys.Length - 1; i++) { if (!currentData.TryGetValue(keys[i], out var tValue)) return defaultValue; - if (tValue is JObject jObject) - currentData = jObject.ToObject>(); - else if (tValue is Dictionary nestedDict) - currentData = nestedDict; - else - return defaultValue; + switch (tValue) + { + case JObject jObject: + currentData = jObject.ToObject>() ?? new Dictionary(); + break; + case Dictionary nestedDict: + currentData = nestedDict; + break; + default: + return defaultValue; + } } if (currentData.TryGetValue(keys[^1], out var result) && result is T typedResult) return typedResult; @@ -61,6 +67,7 @@ public class Config public void Set(string key, T? value) { + LoadConfigData(); var keys = key.Split('.'); var currentData = _configData; for (var i = 0; i < keys.Length - 1; i++)