refactor: 重构配置文件读取逻辑
This commit is contained in:
@@ -41,17 +41,23 @@ public class Config
|
||||
|
||||
public T? Get<T>(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<Dictionary<string, object>>();
|
||||
else if (tValue is Dictionary<string, object> nestedDict)
|
||||
currentData = nestedDict;
|
||||
else
|
||||
return defaultValue;
|
||||
switch (tValue)
|
||||
{
|
||||
case JObject jObject:
|
||||
currentData = jObject.ToObject<Dictionary<string, object>>() ?? new Dictionary<string, object>();
|
||||
break;
|
||||
case Dictionary<string, object> 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<T>(string key, T? value)
|
||||
{
|
||||
LoadConfigData();
|
||||
var keys = key.Split('.');
|
||||
var currentData = _configData;
|
||||
for (var i = 0; i < keys.Length - 1; i++)
|
||||
|
Reference in New Issue
Block a user