Create and Configure an Assembly Object

CCI Metadata

.NET applications must reference the .NET core assembly, mscorlib.dll, and typically reference one or more additional assemblies. Before creating object to represent the assembly, you must load mscorlib.dll by calling the host’s LoadAssembly method. This task must be handled by the host, because the location of assemblies is an application policy issue. The following example shows how HelloIL 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 exposes the default version of mscorlib.dll through the DefaultHost.CoreAssemblySymbolicIdentity property.

For details, see Assemblies and Modules.

Create an Assembly Object

CCI represents assemblies by using the Microsoft.Cci.MutableCodeModel.Assembly object. HelloIL creates a new Assemblyobject 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 Assemblyconstructor has no argument, but you must configure several properties. HelloIL 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: Assemblyand 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 needed. The immutable version is read-only, and you cannot change the underlying object. For details, see Mutable and Immutable Representations.

Configure the Assembly Object

After creating the Assemblyobject, HelloIL 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 IAssemblyReference interface to Assembly.AssemblyReferenced.Add. HelloIL references only mscorlib.dll, which was loaded earlier, so it simply adds the reference, as follows:
assembly.AssemblyReferences.Add(coreAssembly);
Add the Namespace Hierarchy
Assemblies must contain a root namespace, also called the empty namespace. They can optionally contain additional namespace, 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 HelloIL 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 Dec 18, 2009 at 7:58 PM by Guy_Smith, version 2

Comments

No comments yet.