This project is read-only.

Add Method Metadata to the Types

CCI Code Model

After adding type metadata to the assembly, use CCI Metadata to add metadata for the type’s members to the type’s metadata by creating a definition object for each member and adding them to the appropriate NamespaceTypeDefinition object.

Test includes a single member, the Main method, which is also the assembly’s entry point.

For details, see Member Definition Objects.

To add method metadata to a type, create and configure a Microsoft.Cci.MutableCodeMode.MethodDefinition object and add it to the type’s NamespaceTypeDefinition object.

The following example shows how HelloCodeModel adds Main metadata to the Test class’s NamespaceTypeDefinition object.
MethodDefinition mainMethod = new MethodDefinition() {
  ContainingTypeDefinition = testClass,
  InternFactory = host.InternFactory,
  IsCil = true,
  IsStatic = true,
  Name = nameTable.GetNameFor("Main"),
  Type = host.PlatformType.SystemVoid,
  Visibility = TypeMemberVisibility.Public,
assembly.EntryPoint = mainMethod;

To configure the MethodDefinition object, set the following properties:
  • ContainingType: The NamespaceTypeDefinition object for the class that contains the method, Test in this case.
  • InternFactory: An InternFactory object, the same one that was passed to the NamespaceTypeDefinition objects.
  • IsXYZ: Characterize the method by setting the appropriate IsXYZ properties to true.
    • IsCil indicates that Main is implemented in the Common Language Interface (CLI) Common Intermediate Language.
    • IsStatic indicates that Main is a static method.
  • Name: The method IName interface.
  • Type: The method’s return type. Main is a void return type, so HelloCodeModel sets Type to DefaultHost.PlatformType.SystemVoid.
  • Visibility: The method’s visibility. Set this property to a value from the Microsoft.Cci.TypeMemberVisibility enumeration. Main is a public method.
HelloCodeModel then adds the configured MethodDefinition object to the type’s NamespaceTypeDefinition object by passing the MethodDefinition object to NamespaceTypeDefinition.Methods.Add. Because Main is the assembly’s entry point, HelloCodeModel also assigns the Main method’s MethodDefinition object to the Assembly* object’s EntryPoint property. This step is not required for ordinary methods.

Next: Add Code
Return to Beginning

Last edited Mar 9, 2010 at 8:42 PM by Guy_Smith, version 2


No comments yet.