The NamespaceTypeDefinition Object
CCI represents type metadata by a Microsoft.Cci.MutableCodeModel.NameSpaceTypeDefinition
object or the corresponding interface,
. The following example from the
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"),
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.
belongs to the
, so the example sets the ContainingUnitNamespace
property to the assembly’s
object. Other namespaces are defined by UnitNamespace objects. To add a nested type to its parent, you would create a
object, and assign the parent type to the object’s
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
does for strings. However, types are usually not the same across multiple
, so each host typically has its own InternFactory
object. To share
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
. Other possibilities include IsEnum
, and so on.
The Name Property
The type name’s
is assigned to the Name
property. To obtain an IName
interface, pass the name to
Add Type Metadata to an Assembly
If you are working with a mutable representation to create or modify an assembly, you add a configured
object to the Assembly
object as follows:
- 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.
- Add the type’s configured NamespaceTypeDefinition object to Assembly.AllTypes, which is a flat list of every type in the assembly.
- 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.