Create and Configure an Assembly Object

CCI Code Model

CCI represents assemblies by using the Microsoft.Cci.MutableCodeModel.Assembly object, which is part of the CCI Metadata API. Before creating the object, you must first load mscorlib.dll by calling the host’s LoadAssembly method. This task must be handled by the host, because the location of DLLs and the appropriate version is an application policy issue. The following example shows how HelloCodeModel loads the core assembly.
IAssembly coreAssembly = host.LoadAssembly(host.CoreAssemblySymbolicIdentity);

LoadAssembly has one input, an AssemblyIdentity object that represents the assembly to be loaded. Because the core assembly must be loaded by all .NET applications, DefaultHost simplifies the process by exposing mscorlib.dll through the DefaultHost.CoreAssemblySymbolicIdentity property. DefaultHost specifies unification for mscorlib.dll, so CoreAssemblySymbolicIdentity contains the most recent version on the system.

Note: The Microsoft.Cci.MutableCodeModel namespace is a somewhat confusing historical artifact. It actually contains the types that support the CCI Metadata mutable representation. The types that support CCI Code are in the Microsoft.Cci.Ast namespace. For more discussion of the mutable metadata model, see Mutable and Immutable Representations.

For more discussion of assemblies, see Assemblies and Modules.

Create an Assembly Object

HelloCodeModel creates a new Assembly object to represent Hello.exe, as follows.
var assembly = new Assembly( ) {
  Name = nameTable.GetNameFor("Hello"),
  ModuleName = nameTable.GetNameFor("hello.exe"),
  Kind = ModuleKind.ConsoleApplication,
  TargetRuntimeVersion = coreAssembly.TargetRuntimeVersion,

The Assembly constructor has no argument, but you must configure several properties. HelloCodeModel configures them as follows.
  • Name: An IName interface for the assembly name, “hello”.
  • ModuleName: An IName interface for the module name, which is typically just the assembly name with an appropriate extension.
  • Kind: A ModuleKind value that indicates the assembly type, a console application in this case.
  • TargetRuntimeVersion: A version string for the targeted .NET version. This example simply uses the version string from the core assembly that was loaded earlier.
Note: Assembly and IAssembly are examples of mutable and immutable versions of a CCI Metadata type. The mutable version is a normal object, and you can change its properties as required. The immutable version is read-only, and you cannot change the underlying object.

Configure the Assembly Object

After creating the Assembly object, HelloCodeModel performs the remaining configuration.

Add Assembly References

You must add references to all appropriate assemblies. To do so, load the assembly by using the host’s LoadAssembly method and pass the corresponding IAssemblyReference interface to Assembly.AssemblyReferenced.Add.

HelloCodeModel references only mscorlib.dll, which was loaded earlier, so it adds assembly references, as follows:

Add the Namespace Hierarchy

Assemblies must contain a root namespace, also called the empty namespace. They can optionally contain additional namespaces, which are organized in a hierarchy under the root namespace. You must add the namespace hierarchy to the assembly.

Hello.exe has only a root namespace, which HelloCodeModel adds to the assembly as follows:
RootUnitNamespace rootUnitNamespace = new RootUnitNamespace();
assembly.UnitNamespaceRoot = rootUnitNamespace;
rootUnitNamespace.Unit = assembly;

To add the root namespace to an assembly, assign Microsoft.Cci.MutableCodeModel.RootUnitNamespace to the assembly’s UnitNamespaceRoot property and assign the assembly object to the RootUnitNamespace object’s Unit property. Namespaces are always doubly-linked in this fashion, to simplify navigation.

For details, see The Root Namespace.

Next: Add Type Metadata to the Assembly
Return to Beginning

Last edited Mar 9, 2010 at 7:23 PM by Guy_Smith, version 1


No comments yet.