dict = ... Color =...">

C #: Cannot cast object of type "System.Int64" for input "System.Int32"

I have code like this:

Dictionary<object, object> dict = ...
Color = (int)dict.GetValue("color");

      

When I convert Color to int, I get the following exception:

System.InvalidCastException: Unable to cast on object of type 'System.Int64' to type 'System.Int32'.

I'm not sure why I can't just convert from long to int. I know the value is less 0xFFFFFF

(24 bits) since it is a color.

I tried to use unchecked

but that didn't work either.

+3


source to share


1 answer


First you have to unbox the value as a dictionary type value object

.



Dictionary<object, object> dict = ...
Color = (int)(long)dict.GetValue("color");

      

+7


source







All Articles