The NamespaceTypeDefinition Object

CCI Metadata

CCI represents type metadata by a Microsoft.Cci.MutableCodeModel.NameSpaceTypeDefinition object or the corresponding interface, INameSpaceTypeDefinition. The following example from the HelloIL sample creates and configures the metadata for the Test class, and adds it to an 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 following sections describe the details.

The ContainingUnitNamespace Property

A type can belong to a namespace or, in the case of a nested type, to another type. Test belongs to the root namespace, so the example sets the ContainingUnitNamespace property to the assembly’s RootUnitNamespace object. Other namespaces are defined by UnitNamespace objects. To add a nested type to its parent, you would create a NestedTypeDefinition object, and assign the parent type to the object’s ContainingTypeDefinition property.

The InternFactory Property

It’s often necessary to test types for equality. If there is only one object per type, you can simply test the object identities. However, type references might not be to the same object. To determine whether two objects reference the same type, you must compare object structure rather than object identity, which is an expensive process.

To improve performance, CCI Metadata associates each type with an InternFactory object, which evaluates the type’s structure and assigns it a unique key. The structural evaluation is done only once, and then types can be compared through their keys.

In a general sense, InternFactory serves the same purpose for types that NameTable does for strings. However, types are usually not the same across multiple hosts, so each host typically has its own InternFactory object. To share InternFactory objects across hosts, you must implement your own host and provide a sharing mechanism.

The IsXYZ Property

The appropriate IsXYZ property is set to true to define the kind of type. Test is a class, so the example sets IsClass to true. Other possibilities include IsEnum, IsInterface, IsDelegate, and so on.

The Name Property

The type name’s IName interface is assigned to the Name property. To obtain an IName interface, pass the name to NameTable.GetNameFor.

Add Type Metadata to an Assembly

If you are working with a mutable representation to create or modify an assembly, you add a configured NamespaceTypeDefinition object to the Assembly object as follows:
  1. Add the type to the appropriate namespace definition object’s Members property, which is a list of the types in the namespace, by passing the configured NamespaceTypeDefinition object to Members.Add. The example adds Test to the root namespace’s definition object, RootUnitNamespace.
  2. Add the type’s configured NamespaceTypeDefinition object to Assembly.AllTypes, which is a flat list of every type in the assembly.
  3. Specify the type’s parent class. The NamespaceTypeDefinition object’s BaseClasses property is a list of the type’s parent classes or interfaces. To add a class or interface to the list, pass the associated ITypeReference interface to BaseClasses.Add. Test inherits from System.Object. Because the identity of System.Object is a matter of application policy, you obtain its ITypeReference interface from DefaultHost.PlatformType.SystemObject.PlatformType. This object is basically a collection of references to core types such as System.Object and System.Int32.

Last edited Mar 17, 2010 at 3:25 PM by Guy_Smith, version 3

Comments

No comments yet.