This project is read-only.

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 MethodDefinition.Body property. The code block is represented by an IMethodBody interface, and the interface’s Operations 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 Microsoft.Cci.ILGenerator class, which simplifies the task of creating method bodies and generating MSIL instructions. For an example of how to use ILGenerator, 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 SourceMethodBody objects. The SourceMethodBody.Block property contains a BlockStatement 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 for loop.
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 Assembly object and populate it with NamespaceTypeDefinition and MethodDefinition objects to represent type and method metadata.

For example, to create a method body and code block for a method, you:
  1. Create a SourceMethodBody to represent the method body.
  2. Create a BlockStatement object to represent the code block.
  3. Use expression and statement objects to construct the lines of code that make up the code block and add them to the BlockStatement.Statements property.
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:
  1. Use CCI Metadata to load the assembly into an Assembly object.
  2. 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.
  3. Use techniques such as those discussed in the FindGeneric Sample walk through the assembly’s types and methods until you reach the appropriate MethodDefinition object.
  4. 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.

Next: The CCI Metadata Object Model
Return to Beginning

Last edited Jan 21, 2010 at 6:31 PM by Guy_Smith, version 1


No comments yet.