Seems as though casting is not transitive in some cases. Consider trying to get an Integer from an
SObject number field.
Integer myInt = (Integer) mySObject.get('My_Field__c');
System.TypeException: Invalid conversion from runtime type Decimal to Integer
Apparently you need to first cast it to a Decimal, so you can then Cast it to a Integer.
Integer myInt = (Integer)(Decimal) mySObject.get('My_Field__c');
This seems broken to me. Pretty sure casting should be transitive. In other words if.
A=B & B=C then A=C.
Or maybe I'm missing something?