Skip to content

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

License

Notifications You must be signed in to change notification settings

MSDN-WhiteKnight/CilTools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ff089fe · Sep 24, 2024
Sep 24, 2024
Mar 17, 2024
Sep 11, 2024
Mar 17, 2024
Feb 10, 2024
Oct 1, 2023
Mar 17, 2024
Dec 19, 2020
Sep 14, 2024
Sep 19, 2024
Sep 14, 2024
Dec 11, 2021
Mar 20, 2024
Mar 20, 2024
Mar 20, 2024
Mar 16, 2024
Jun 21, 2020
Jun 21, 2020
Sep 11, 2024
Feb 10, 2024
Jul 2, 2023
Dec 3, 2023
Jul 2, 2023

Repository files navigation

CIL Tools

logo

License: BSD 2.0

gitflic-catalog   GitHub release (latest by date)

Documentation | Examples

CIL tools is a set of software to work with Common Intermediate Language in .NET.

Libraries:

Applications:

Using CilTools.BytecodeAnalysis

Requirements: .NET Framework 3.5+ or .NET Standard 2.0+

Nuget

CilTools.BytecodeAnalysis reads .NET methods' Common Intermediate Language (CIL) bytecode and converts it into high-level objects or textual CIL representation so they can be easily studied and programmatically processed.

Usage

Add reference to CilTools.BytecodeAnalysis.dll, import CilTools.BytecodeAnalysis namespace. Use CilReader.GetInstructions to get the collection of instructions from method, CilGraph.Create to get a graph that represents a flow of control between method's instructions, or CilAnalysis.MethodToText when you need to output method's CIL code as text. CilTools.BytecodeAnalysis.Extensions namespace provides an alternative syntax via extension methods.

Example

using System;
using System.Collections.Generic;
using CilTools.BytecodeAnalysis;
using CilTools.BytecodeAnalysis.Extensions;

namespace CilToolsExample
{
    class Program
    {
        public static void Hello()
        {
            int a = 1;
            int b = 2;
            Console.WriteLine("Hello, World");
            Console.WriteLine("{0} + {1} = {2}",a,b,a+b);
        }

        static void Main(string[] args)
        {
            IEnumerable<CilInstruction> instructions = typeof(Program).GetMethod("Hello").GetInstructions();

            foreach (CilInstruction instr in instructions)
            {
                Console.WriteLine(instr.ToString());
            }
            Console.ReadKey();
        }

    }
}

/* Output:

nop
ldc.i4.1
stloc.0
ldc.i4.2
stloc.1
ldstr      "Hello, World"
call       void [mscorlib]System.Console::WriteLine(string)
nop
ldstr      "{0} \053 {1} \075 {2}"
ldloc.0
box        [mscorlib]System.Int32
ldloc.1
box        [mscorlib]System.Int32
ldloc.0
ldloc.1
add
box        [mscorlib]System.Int32
call       void [mscorlib]System.Console::WriteLine(string, object, object, object)
nop
ret
*/

For more information, see documentation.


Copyright (c) 2023, SmallSoft