From a11b29a21d5564c8b90223e9b6c1ca6ce8f18154 Mon Sep 17 00:00:00 2001 From: Jack <3486688394@qq.com> Date: Tue, 21 Jan 2025 17:59:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E8=AF=BB=E5=8F=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JackCraft.Config/Config.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) 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++)