namespace Milimoe.FunGame.Core.Api.Utility { public static class DictionaryExtensions { /// /// 从 中获取指定键的值,并将其转换为指定的类型。 /// /// 要转换的目标类型 /// 要从中获取值的字典 /// 要获取值的键 /// 转换后的值,如果键不存在或转换失败,则返回默认值。 public static T? GetValue(this Dictionary dict, string key) { if (dict is null || !dict.TryGetValue(key, out object? value) || value is null) { return default; } try { // 如果已经是目标类型,则直接返回 if (value is T t) { return t; } return NetworkUtility.JsonDeserializeFromDictionary(dict, key); } catch (Exception) { return default; } } } }