MethodBodyBlock Class

Definition

Represents the method body in ECMA 335 assembly.

public ref class MethodBodyBlock sealed
public sealed class MethodBodyBlock
type MethodBodyBlock = class
Public NotInheritable Class MethodBodyBlock
Inheritance
MethodBodyBlock

Examples

This example shows how to read method bodies for all methods in the specified type definition and display method body information:

static void PrintMethods(PEReader reader, MetadataReader mr, TypeDefinition tdef)
{
    MethodDefinitionHandleCollection methods = tdef.GetMethods();

    foreach (MethodDefinitionHandle mdefh in methods)
    {
        MethodDefinition mdef = mr.GetMethodDefinition(mdefh);
        string mname = mr.GetString(mdef.Name);
        Console.WriteLine($"Method: {mname}");

        // Get the relative address of the method body in the executable
        int rva = mdef.RelativeVirtualAddress;

        if (rva == 0)
        {
            Console.WriteLine("Method body not found");
            Console.WriteLine();
            continue;
        }

        // Get method body information
        MethodBodyBlock mb = reader.GetMethodBody(rva);
        Console.WriteLine($"  Maximum stack size: {mb.MaxStack}");
        Console.WriteLine($"  Local variables initialized: {mb.LocalVariablesInitialized}");

        byte[]? il = mb.GetILBytes();
        Console.WriteLine($"  Method body size: {il?.Length ?? 0}");
        Console.WriteLine($"  Exception regions: {mb.ExceptionRegions.Length}");
        Console.WriteLine();

        foreach (var region in mb.ExceptionRegions)
        {
            Console.WriteLine(region.Kind.ToString());
            Console.WriteLine($"  Try block offset: {region.TryOffset}");
            Console.WriteLine($"  Try block length: {region.TryLength}");
            Console.WriteLine($"  Handler offset: {region.HandlerOffset}");
            Console.WriteLine($"  Handler length: {region.HandlerLength}");
            Console.WriteLine();
        }
    }
}

Remarks

The method body contains Common Intermediate Language (CIL) instructions that make up a method and information about its local variables and exception regions. You can use the GetMethodBody method to get a MethodBodyBlock instance for the specified method.

The format of CIL instructions and metadata is defined by the ECMA-335 specification. For more information, see Standard ECMA-335 - Common Language Infrastructure (CLI) on the Ecma International Web site.

Properties

ExceptionRegions

Gets the array of exception regions in this method body.

LocalSignature

Gets the handle to the local variables signature.

LocalVariablesInitialized

Gets a value that indicates whether local variables in this method are initialized to default values of their types.

MaxStack

Gets the maximum number of items on the evaluation stack for this method.

Size

Gets the size of the method body, including the header, IL, and exception regions.

Methods

Create(BlobReader)

Creates a new instance of the MethodBodyBlock class using the specified blob reader.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetILBytes()

Gets the IL bytecode of this method body as a byte array.

GetILContent()

Gets the IL bytecode of this method body as an immutable array.

GetILReader()

Gets a blob reader that reads the IL bytecode of this method body.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to