我有如下代码:
Dictionary<object, object> dict = ...
Color = (int)dict.GetValue("color");
当我将 Color 转换为 int 时,出现以下异常:
System.InvalidCastException: Unable to cast object of type 'System.Int64' to type 'System.Int32'.
我不确定为什么不能将 long 型转换为 int 型。我知道该值小于 0xFFFFFF
(24 位),因为它是一种颜色。
我尝试使用 unchecked
但这也没有帮助。
最佳答案
你必须先unbox作为字典值类型的值是object
。
Dictionary<object, object> dict = ...
Color = (int)(long)dict.GetValue("color");
关于C#:无法将类型为 'System.Int64' 的对象转换为类型 'System.Int32',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31737950/