This project is read-only.

Add Type Metadata to the Assembly

CCI Metadata

After you create and configure the assembly, add the metadata for each type in the assembly. Hello.exe contains two types, <Module> and Test.

Add the <Module> Metadata

Every assembly must contain a <Module> type, which serves as a container for anything that doesn’t belong to a class. <Module> is always a child of the empty namespace and the first type in the assembly.

To add type metadata to an assembly:
  • Create a Microsoft.Cci.MutableCodeMode.NameSpaceTypeDefinition object and add it to the Assemblyobject’s AllTypes property. AllTypes is a flat list of every type in the assembly.
For details, see The NamespaceTypeDefinition Object.

The following example from HelloIL creates and configures a <Module> type and adds it to the hello assembly.
NamespaceTypeDefinition moduleClass = new NamespaceTypeDefinition() {
  ContainingUnitNamespace = rootUnitNamespace,
  InternFactory = host.InternFactory,
  IsClass = true,
  Name = nameTable.GetNameFor("<Module>"),

To configure the object, set the following properties:
  • ContainingUnitNamespace: The type’s parent namespace. <Module> belongs to the root namespace, so HelloIL sets the ContainingUnitNamespace* property to the RootUnitNamespace object created earlier.
  • InternFactory: An InternFactory object associates types with unique integers. InternFactory serves the same purpose for types that NameTable does for strings; it speeds up the process of determining whether two objects are the same type.
  • IsXYZ: Set the appropriate IsXYZ property to true to indicate what kind of type is being added. <Module> is a class, so the example sets IsClass to true.
  • Name: The type name’s IName interface.
Add the configured NamespaceTypeDefinition object to the Assemblyobject’s metadata by passing it to AllTypes.Add. AllTypes is a flat list of all the types in the assembly.

Add the Test Class

Hello.exe has one application-defined class, Test. The following code creates a NameSpaceTypeDefinition object to represent Test and adds it to the assembly.
NamespaceTypeDefinition testClass = new NamespaceTypeDefinition()
  ContainingUnitNamespace = rootUnitNamespace,
  InternFactory = host.InternFactory,
  IsClass = true, 
  Name = nameTable.GetNameFor("Test"),
  IsPublic = true,  

The configuration is similar to that for <Module>.
  • HelloIL adds the Test type to the root namespace, so ContainingUnitNamespace is set to RootUnitNamespace object.
  • All the types handled by this host use the same InternFactory.
  • Test is a class, so IsClass is set to true.
  • HelloIL specifies “Test” as the type name and adds it to the NameTable object.
  • Test is public, so IsPublic is set to true.
Test is a normal class, so in addition to adding the NamespaceTypeDefinition object to AllTypes, you must also add the type to the namespace definition object of the parent namespace and specify the type’s parent class. HelloIL:
  • Adds the NamespaceTypeDefinition object to the RootUnitNamespace object’s Members property, which is a list of The types in the namespace.
  • Specifies the type’s parent class, by adding it to the NamespaceTypeDefinition object’s BaseClasses property. Test inherits from System.Object. Because the identity of Object is a matter of application policy, HelloIL obtains the ITypeReference interface from DefaultHost.PlatformType.SystemObject. PlatformType.

Next Add Method Metadata to the Types
Return to Beginning

Last edited Dec 18, 2009 at 9:22 PM by Guy_Smith, version 3


No comments yet.