我正在尝试将键传递给(对象的)字典并获取值(可能是各种类型)或回退到我提供的默认值。
例如
// 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 (T)settingsDictionary[key];
这消除了编译错误,但我有 InvalidCastExpections。例如,在 json 中,数字存储为 35.0(这将是一个 double ),如果我调用:
GetValueFromSettings("someOffset", 32.0f);
当它在 json 中找到 32.0 键并尝试转换为 float 时,我会得到一个 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() 但还是没有运气。
如有任何帮助,我们将不胜感激。
最佳答案
您看到的(在第一种情况下)是您无法从 int
中拆箱至 float
.你在转换字典本身时看到的是 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;
这将处理更多的转换 - 但如果没有合适的可用转换,它将抛出异常。
关于c# - 从字典返回通用类型(否则返回默认通用值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28610477/