Search code examples
c#linuxmonocil

View CIL of C# code on Linux with mono


I want to inspect the generated CIL code of the following C# source file on Linux:

using System;
namespace PrintPrimes
{
    class Algorithm
    {
        static bool IsPrime(int p)
        {
            for (int i=2;i<p;i++)
            {
                for (int j=i;j<p;j++)
                {
                    if (i*j == p)
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        static void Main() 
        {
            for (int p=2;p<=20;p++)
            {
                if (IsPrime(p))
                {
                    Console.WriteLine(p + " ");
                }
            }
        }
    }
}

When I compile and run it everything is fine:

$ mcs -out:PrintPrimes PrintPrimes.cs
$ ./PrintPrimes
2 
3 
5 
7 
11 
13 
17 
19 

But how can I get the human readable CIL output?


Solution

  • Mono disassembler, extracts IL code from an assembly:

    monodis FILE-name
    

    Full reference can be found here: https://www.mono-project.com/docs/tools+libraries/tools/monodis/

    Dis/Assembling CIL Code (Mono Project)