CCI and Code
You can work an assembly’s code by using either the CCI Metadata or CCI Code Models.
Code Generation with CCI Metadata
CCI Metadata uses a MethodDefinition
object to represent a method’s metadata. You can access the associated code block through the
property. The code block is represented by an IMethodBody
interface, and the interface’s
property contains the list of MSIL instructions that make up the code block.
You can use CCI Metadata to generate MSIL for method bodies. Applications typically generate MSIL code blocks by using the
class, which simplifies the task of creating method bodies and generating MSIL instructions. For an example of how to use
, see the
HelloIL Sample Walkthrough
Code Generation with CCI Code
CCI Code represents code in a structured form that is similar to source code and much more flexible to work with. CCI Code bodies are represented by
objects. The SourceMethodBody.Block property contains a
object that represents the code block, and BlockStatement.Statements
contains a collection of the statement objects that make up the code block.
The CCI Code object model consists of objects that represent.NET expressions and statements and are used to represent a code block. For example:
- ForStatement represents a for loop.
- The ForStatement.Condition property contains an expression object, such as
LessThan, that represents the condition to be evaluated for each iteration of the loop.
- ForStatement.Body contains a BlockStatement object that represents the body of the
There are two basic ways to use CCI Code: create new code blocks, or analyze existing code blocks.
Create Code Blocks
If you are constructing an assembly from scratch, or want to add code to an existing assembly, you can create code blocks from scratch. For example, to create an assembly from scratch, you must first use CCI Metadata to create an
object and populate it with NamespaceTypeDefinition
objects to represent type and method metadata.
For example, to create a method body and code block for a method, you:
- Create a SourceMethodBody to represent the method body.
- Create a BlockStatement object to represent the code block.
- Use expression and statement objects to construct the lines of code that make up the code block and add them to the
For a simple example of this process, see the HelloCodeModel sample in the CCI Code source tree.
Work with Existing Code Blocks
You can use the following general procedure analyze or modify existing code:
Next: The CCI Metadata Object Model
Return to Beginning
- Use CCI Metadata to load the assembly into an Assembly object.
- Use the Microsoft.Cci.IlToCodeModel.Decompiler.GetCodeModelFromMetadataModel method to construct a code model for the assembly’s types by decompiling the MSIL.
GetCodeModelFromMetadataModel returns a mutable metadata model that is equivalent to the original. However, the the method bodies also implement
ISourceMethodBody, which provides access to the CCI Code representation of the code block.
- Use techniques such as those discussed in the
FindGeneric Sample Walkthrough.to walk through the assembly’s types and methods until you reach the appropriate
- MethodDefinition.Body contains the object that represents the method body, and the object’s
ISourceMethodBody.Block property contains the statement objects that represent the code in the code block.