C#对象未解析为int

C#对象未解析为int,c#,casting,C#,Casting,我的代码如下所示: var score = ((Dictionary<string, object>)entry.Value)["score"]; print("score: " + score + "!"); FBManager.instance.friends[id].score = (int) score; //OUTPUT: //score: 5! //InvalidCastException: Specified cast is not valid. var sco

我的代码如下所示:

var score = ((Dictionary<string, object>)entry.Value)["score"];
print("score: " + score + "!");
FBManager.instance.friends[id].score = (int) score;


//OUTPUT: 
//score: 5!
//InvalidCastException: Specified cast is not valid.
var score=((字典)entry.Value)[“score”];
打印(“分数:“+分数+”!”);
FBManager.instance.friends[id].score=(int)score;
//输出:
//得分:5分!
//InvalidCastException:指定的强制转换无效。
为什么我不能将
score
转换为int


我很确定
entry.Value
是一本
字典,因为我打印了
entry.Value.GetType()
,它说的正是这一点。

看看当你这样做时会得到什么:
Console.WriteLine(score.GetType())

如果是System.String,则需要转换(convert.ToInt32(score))或int.Parse(score)。如果是System.Int32,则可以进行强制转换。如果是其他值(双精度、十进制等),只需进行转换:)


祝你好运

您的值不太可能是装箱的
int
它可能是十进制、双精度还是字符串?分数不会是int。请尝试转换.ToInt32(分数)或解析它,只需执行
int.parse(分数)有关将对象解析为int:Convert.ToInt32(score)的示例,请参见此问题!我从firebase中以“数字”的形式检索这个分数,我认为正常情况下可以将其转换为int,但似乎我错了。请随意写下来作为答案。