Value Type Conversion in Dynamically Generated IL

后端 未结 2 509
耶瑟儿~
耶瑟儿~ 2021-02-08 06:00

Update
Over a year later, and I finally realized the cause of this behavior. Essentially, an object can\'t be unboxed to a different type

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-08 06:33

    In order to unbox the value, you must first have boxed it, and for the unbox to not throw you must have converted the value to the type you unbox it to before you boxed it.

    However, as the type of the property setter is known, and you're dealing with value types, you shouldn't have to box/unbox at all:

    E.g. if you wanted to call a property setter of type Int32 with an Int64 argument, it would go something like this:

    // Int 64 argument value assumed on top of stack now
    conv.i4  // convert it to int32
    callvirt   ...
    

提交回复
热议问题