这个问题涉及cli的定义,
参考:
Common Language Infrastructure (CLI)
Partition III
CIL Instruction Set
Final Draft, Apr 2005
---------------------------------------------------------------------------------
In the following table, “CLI Type” is the type as it is described in metadata. The “Verification Type” is a corresponding type used for type compatibility rules in verification (see §1.8.1.2.2) when considering the types of local variables, arguments, and parameters on methods being called. The column “Verification Type (in stack state)” is used to simulate instructions that load data onto the stack, and shows the types that are actually maintained in the stack state information of the verification algorithm. The column “Managed Pointer to Type” shows the type tracked for managed pointers.
CLI Type Verification Type Verification Type (in stack state) Managed Pointer to Type
int8, unsigned int8, bool int8 int32 int8&
int16, unsigned int16, char int16 int32 int16&
int32, unsigned int32 int32 int32 int32&
int64, unsigned int64 int64 int64 int64&
native int, native unsigned int native int native int native int&
float32 float32 float64 float32&
float64 float64 float64 float64&
Any value type Same type Same type Same type&
Any object type Same type Same type Same type&
Method pointer Same type Same type Not valid
These operate only on integer types. Used for and, div.un, not, or, rem.un, xor. The div.un and rem.un instructions treat their operands as unsigned integers and produce the bit pattern corresponding to the unsigned result. As described in the CLI standard, however, the CLI makes no distinction between signed and unsigned integers on the stack. The not instruction is unary and returns the same type as the input. The shl and shr instructions return the same type as their first operand, and their second operand shall be of type int32 or native int. Boxes marked X indicate invalid CIL sequences. All other boxes denote verifiable combinations of operands.
Table 5: Integer Operations
int32 int64 native int F & O
int32 int32 X native int X X X
int64 X int64 X X X X
native int native int X native int X X X
--------------------------------------------------------------------------------------
注意第一张表的Verification Type (in stack state)列,真正用于计算的是这一列的数据
根据第一张表,所有的byte,sbyte,short,ushort都会被转成int类型进行计算
再根据第二张表,int op(+-*/%) int的结果是int
把int的值赋值给short的变量,自然需要强制转换