Add Method Metadata to the Types

CCI Metadata

After adding type metadata to the assembly, 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 type metadata. 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 HelloIL adds Main metadata to the Test class’s NamespaceTypeDefinition object.
MethodDefinition mainMethod = new MethodDefinition() {
  ContainingType = testClass,
  InternFactory = host.InternFactory,
  IsCil = true,
  IsStatic = true,
  Name = nameTable.GetNameFor("Main"),
  Type = host.PlatformType.SystemVoid,
  Visibility = TypeMemberVisibility.Public,
};
assembly.EntryPoint = mainMethod;
testClass.Methods.Add(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 name’s IName interface.
  • Type: The method’s return type. Main is a void return type, so HelloIL 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.
HelloIL 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, HelloIL also assigns the Main method’s MethodDefinition object to the Assemblyobject’s EntryPoint property. This step is not required for ordinary methods.

Next: Add Code to the Method Bodies
Return to Beginning

Last edited Dec 18, 2009 at 8:25 PM by Guy_Smith, version 4

Comments

No comments yet.