Return 来自字典的通用类型(否则 return 默认通用值)

Return generic type from dictionary (else return a default generic value)

我正在尝试将键传递给(对象的)字典并获取值(可能是各种类型)或回退到我提供的默认值。

例如

// Called from some other method
// It should look in the dictionary for that key and return the value
// else return the int 365
GetValueFromSetting("numDaysInYear", 365)

public static T GetValueFromSettings<T>(string key, T defaultValue)
{
    // Settings is a dictionary, which I get in json form
    Dictionary<string, object> settingsDictionary = (Dictionary<string, object>)ParseConfig.CurrentConfig.Get<Dictionary<string, object>>("settings");

    if(settingsDictionary.ContainsKey(key))
    {
        return settingsDictionary[key];
    }

    return defaultValue;
}   

首先我得到了。无法将类型对象隐式转换为 T。存在显式转换(是否缺少强制转换?)

所以我用

转换了密钥 return
return (T)settingsDictionary[key];

这消除了编译错误,但我有 InvalidCastExpections。例如,在 json 中,数字存储为 35.0(这将是一个双精度数),如果我调用:

GetValueFromSettings("someOffset", 32.0f);

当它在 json 中找到密钥为 32.0 并尝试转换为浮点数时,我会得到一个 InvalidCastExpection。

我也试过使用泛型而不是对象:

public static T GetValueFromSettings<T>(string key, T defaultValue)
{
    // Settings is a dictionary, which I get in json form
    Dictionary<string, T> settingsDictionary = (Dictionary<string, T>)ParseConfig.CurrentConfig.Get<Dictionary<string, T>>("settings");

    if(settingsDictionary.ContainsKey(key))
    {
        return settingsDictionary[key];
    }

    return defaultValue;
}   

希望它能修复它,但这也会导致无效的强制转换异常。这次它出现在字典中,因为 json 需要一种字典。

我也看到了 System.Convert.ChangeType() 但还是没有运气。

如有任何帮助,我们将不胜感激。

您看到的(在第一种情况下)是您无法从 intfloat 拆箱。你在转换字典本身时看到的是 Dictionary<string, object> 不是 Dictionary<string, float>,这对我来说似乎完全合理。

您可能想使用:

// I don't *expect* that you need a cast herem, given the type argument
var settingsDictionary = ParseConfig.CurrentConfig.Get<Dictionary<string, object>>("settings");
object value;
if (!settingsDictionary.TryGetValue(key, out value))
{
    return defaultValue;
}
object converted = Convert.ChangeType(value, typeof(T));
return (T) converted;

这将处理更多的转换 - 但如果没有合适的可用转换,它将引发异常。