我的代码如下:
Dictionary<object, object> dict = ...
Color = (int)dict.GetValue("color");
当我将 Color 转换为 int 时,出现以下异常:
System.InvalidCastException:无法转换类型的对象
“System.Int64”以键入“System.Int32”。
我不知道为什么我不能从 long 转换为 int 。我知道该值小于0xFFFFFF
(24 位),因为它是一种颜色。
我尝试使用unchecked
但这也没有帮助。
你必须首先unbox https://msdn.microsoft.com/en-us/library/yz2be5wk.aspx作为字典的值类型的值是object
.
Dictionary<object, object> dict = ...
Color = (int)(long)dict.GetValue("color");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)