use*_*934 1 c# generics json dictionary
我试图将键传递给(对象的)字典并获取值(可能是各种类型)或回退到我提供的默认值。
例如
// 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;
}
Run Code Online (Sandbox Code Playgroud)
首先我得到了。无法将类型对象隐式转换为 T。存在显式转换(是否缺少强制转换?)
所以我用回车键
return (T)settingsDictionary[key];
Run Code Online (Sandbox Code Playgroud)
这消除了编译错误,但我有 InvalidCastExpections。例如,在 json 中,数字存储为 35.0 (这将是一个双精度),如果我调用:
GetValueFromSettings("someOffset", 32.0f);
Run Code Online (Sandbox Code Playgroud)
当它发现 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;
}
Run Code Online (Sandbox Code Playgroud)
希望它能修复它,但这也会导致无效的强制转换异常。这次它在字典上,因为 json 需要字典类型。
我也见过 System.Convert.ChangeType() 但再次没有运气。
任何帮助,将不胜感激。
您所看到的(在第一种情况下)是您无法从intto拆箱float。在转换字典本身时,您看到的是 aDictionary<string, object>不是 a 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;
Run Code Online (Sandbox Code Playgroud)
这将处理更多的转换 - 但如果没有合适的转换可用,它将抛出异常。
| 归档时间: |
|
| 查看次数: |
1308 次 |
| 最近记录: |