Class CilGraph
Represents a CIL graph, a graph that reflects a flow of control between CIL instructions in the method
Inheritance
Inherited Members
Namespace: CilTools.BytecodeAnalysis
Assembly: CilTools.BytecodeAnalysis.dll
Syntax
public class CilGraph
Remarks
CIL graph is a directed graph with nodes representing CIL instructions withing method body and edges representing how control flows between them when runtime executes method. The root of the graph is the first instruction of the method. Each node stores a reference to the next instruction (which is usually executed after it) and, if it's a jump instruction, a reference to the branch target (an instruction that would be executed if the condition for the jump is met). For convenience, each instruction serving as branch target is assigned a label, a string that identify it. The last instruction of the method has null as its next instruction reference.
Use Create(MethodBase) method to create CIL graph for a method.
Properties
Method
Gets a method for which this graph is built
Declaration
public MethodBase Method { get; }
Property Value
Type | Description |
---|---|
System.Reflection.MethodBase |
Root
Gets a root node of this graph (the first instruction in the method)
Declaration
public CilGraphNode Root { get; }
Property Value
Type | Description |
---|---|
CilGraphNode |
Methods
Create(MethodBase)
Returns CilGraph that represents a specified method
Declaration
public static CilGraph Create(MethodBase m)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MethodBase | m | Method for which to build CIL graph |
Returns
Type | Description |
---|---|
CilGraph | CIL graph object |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Source method is null |
EmitTo(ILGenerator, Func<CilInstruction, Boolean>)
Emits the entire content of this CIL graph into the specified IL generator, optionally calling user callback for each processed instruction.
Declaration
public void EmitTo(ILGenerator gen, Func<CilInstruction, bool> callback = null)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.Emit.ILGenerator | gen | Target IL generator. |
System.Func<CilInstruction, System.Boolean> | callback | User callback to be called for each processed instruction. |
Remarks
Passing user callback into this method enables you to filter instructions that you want to be emitted into target IL generator. Return true to skip emitting instruction, or false to emit instruction.
GetHandlerNodes(ExceptionBlock)
Gets the collection of nodes that make up the handler of the specified exception block
Declaration
public IEnumerable<CilGraphNode> GetHandlerNodes(ExceptionBlock block)
Parameters
Type | Name | Description |
---|---|---|
ExceptionBlock | block | The exception block to get handler |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<CilGraphNode> | The collection of nodes corresponding to exception block |
Remarks
The exception block must belong to the method from which this graph was created. If the block belongs to another method, the behaviour is undefined. You can get exception blocks that enclose the given graph node using GetExceptionBlocks() method.
GetInstructions()
Enumerates all instructions represented by this graph's nodes
Declaration
public IEnumerable<CilInstruction> GetInstructions()
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<CilInstruction> | The collection of instructions |
GetNodes()
Enumerates nodes in this graph
Declaration
public IEnumerable<CilGraphNode> GetNodes()
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<CilGraphNode> | The collection of graph nodes |
Print(TextWriter, Boolean, Boolean, Boolean, Boolean)
Writes the CIL code corresponding to this graph into the specified TextWriter, optionally including signature, default parameter values, attributes and method header
Declaration
public void Print(TextWriter output = null, bool IncludeSignature = false, bool IncludeDefaults = false, bool IncludeAttributes = false, bool IncludeHeader = false)
Parameters
Type | Name | Description |
---|---|---|
System.IO.TextWriter | output | The destination TextWriter, or null to use standard output |
System.Boolean | IncludeSignature | Indicates that method signature should be included in the output |
System.Boolean | IncludeDefaults | Indicates that default parameter values should be included in the output |
System.Boolean | IncludeAttributes | Indicates that custom attributes should be included in the output |
System.Boolean | IncludeHeader | Indicates that method header should be included in the output |
Remarks
Method header contains information such as maximum stack size and local variable types.
PrintAttributes(TextWriter)
Writes custom attributes of the method represented by this graph into the specified TextWriter
Declaration
public void PrintAttributes(TextWriter output)
Parameters
Type | Name | Description |
---|---|---|
System.IO.TextWriter | output | The destination TextWriter |
PrintDefaults(TextWriter)
Writes default parameter values of the method represented by this graph into the specified TextWriter
Declaration
public void PrintDefaults(TextWriter output)
Parameters
Type | Name | Description |
---|---|---|
System.IO.TextWriter | output | The destination TextWriter |
PrintHeader(TextWriter)
Writes the method header code of the method represented by this graph into the specified TextWriter
Declaration
public void PrintHeader(TextWriter output)
Parameters
Type | Name | Description |
---|---|---|
System.IO.TextWriter | output | The destination TextWriter |
Remarks
The method header in CLI contains information such as stack size and local variables.
PrintSignature(TextWriter)
Writes the signature of the method represented by this graph into the specified TextWriter
Declaration
public void PrintSignature(TextWriter output)
Parameters
Type | Name | Description |
---|---|---|
System.IO.TextWriter | output | The destination TextWriter |
ToString()
Returns the signature of the method represented by this graph
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
System.String | The string with method signature |
Overrides
ToSyntaxTree()
Gets the syntax tree for the method represented by this graph using default disassembler parameters
Declaration
public MethodDefSyntax ToSyntaxTree()
Returns
Type | Description |
---|---|
MethodDefSyntax | The root method definition node of the syntax tree |
ToSyntaxTree(DisassemblerParams)
Gets the syntax tree for the method represented by this graph using specified disassembler parameters
Declaration
public MethodDefSyntax ToSyntaxTree(DisassemblerParams pars)
Parameters
Type | Name | Description |
---|---|---|
DisassemblerParams | pars |
Returns
Type | Description |
---|---|
MethodDefSyntax | The root method definition node of the syntax tree |
ToText()
Returns CIL code corresponding to this graph as a string
Declaration
public string ToText()
Returns
Type | Description |
---|---|
System.String | A string of CIL code |