MSDN-WhiteKnight / CilTools

A set of tools to work with CIL in .NET applications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support for constrained. instruction prefix

MSDN-WhiteKnight opened this issue · comments

There's a constrined. prefix which is emitted by compilers on virtual method calls when the type comes from generic argument which can be a struct. CilInstruction class should be updated to properly parse its operand. Example:

using System;
using System.Reflection;
using CilBytecodeParser;

class Program
{
    public static void Test<T>(T x)
    {        
        Console.WriteLine(x.ToString());
    }

    public static void Main(string[] args)
    {
        Console.WriteLine(CilAnalysis.MethodToText(typeof(Program).GetMethod("Test")));
        Console.ReadKey();
    }
}

Current output:

.method public hidebysig static void Test<T>(
    !!T x
) cil managed {
 .maxstack 8

          nop
          ldarga.s   x
          constrained. 452984833
          callvirt   instance string [mscorlib]System.Object::ToString()
          call       void [mscorlib]System.Console::WriteLine(string)
          nop
          ret
}

Expected output:

.method public hidebysig static void Test<T>(
    !!T x
) cil managed {
 .maxstack 8

          nop
          ldarga.s   x
          constrained. !!T
          callvirt   instance string [mscorlib]System.Object::ToString()
          call       void [mscorlib]System.Console::WriteLine(string)
          nop
          ret
}