Add Type Metadata to the Assembly

CCI Code Model

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

Add <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 Assembly object’s AllTypes property. AllTypes is a flat list of every type in the assembly.

For details, see The NamespaceTypeDefinition Object.

The following example from HelloCodeModel 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>"),
};
assembly.AllTypes.Add(moduleClass);

To configure the object, set the following properties:
  • ContainingUnitNamespace: The type’s parent namespace. <Module> belongs to the root namespace, so HelloCodeModel 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 Assembly object’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, 
  IsPublic = true,  
  Name = nameTable.GetNameFor("Test"),
};
rootUnitNamespace.Members.Add(testClass);
assembly.AllTypes.Add(testClass);
testClass.BaseClasses.Add(host.PlatformType.SystemObject);

The configuration is similar to that for <Module>.
  • HelloCodeModel 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.
  • HelloCodeModel specifies “Test” as the type name and adds it to the NameTable object.
  • Test is a public class, 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 parent namespace’s namespace definition object and specify the type’s parent class. HelloCodeModel:
  • Adds the NamespaceTypeDefinition object to the RootUnitNamespace object’s Members property, which is a list of the namespaces and 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, HelloCodeModel obtains the ITypeReference interface from DefaultHost.PlatformType.SystemObject.PlatformType, which contains ITypeReference objects for the standard system objects.

Next Add Method Metadata to the Types
Return to Beginning

Last edited Mar 9, 2010 at 8:28 PM by Guy_Smith, version 1

Comments

No comments yet.