.netcil

Boxing System.RuntimeArgumentHandle


I am working on an interpreter for MSIL code. When it comes to boxing, there are some known restrictions, for example, it's impossible to box byref-like structs.

Regarding System.RuntimeArgumentHandle, I noticed that for .NET Framework (unlike .NET Core), System.RuntimeArgumentHandle is just a struct, so it seems that boxing is allowed. However, the following program is invalid:

locals init (valuetype [mscorlib]System.RuntimeArgumentHandle V_0)

ldloca.s   V_0
initobj    [mscorlib]System.RuntimeArgumentHandle

ldloc V_0
box [mscorlib]System.RuntimeArgumentHandle
pop

Furthermore, if I use an old Visual Studio 2008 and try to compile the following code, it fails with the error "Cannot convert type 'System.RuntimeArgumentHandle' to 'object'":

var o = (object)new RuntimeArgumentHandle();

Why is it not allowed to box RuntimeArgumentHandle, and how does the compiler understand this?


Solution

  • Boxing System.RuntimeArgumentHandle is a special case in the spec. ECMA-335, 5th edition, 8.2.1.1, emphasis added:

    The value types System.RuntimeArgumentHandle and System.ArgIterator (see Partition IV and CIL instruction arglist in Partition III), contain pointers into the VES stack. They can be used for local variable and parameter signatures. The use of these types for fields, method return types, the element type of an array, or in boxing is not verifiable (§8.8). These two types are referred to as byref-like types.