OK. I’ve got a confession to make: I’ve never written a multi-module assembly. But then again, you probably haven’t written one of these either, right? Come on, admit it. There’s no shame in it ;)
Today, I actually had to create one of these things so that I could examine the effects that multi-module assemblies would have on my runtime aspect weaver. Before we get there, let’s quickly review the difference between a module and an assembly.
Assemblies are defined quite nicely in the Framework SDK documentation. You can refer to the documentation for details, but I’m going to define an assembly as a logical module, where a module is a classic Win32 PE file. I call it a logical module because it can be composed of one or more physical modules that are bound together through an assembly manifest.
Most modules that you are likely to encounter (such as the Framework SDK assemblies) are single module assemblies. The manifest, metadata, code, and resources all live inside of a single Win32 PE file.
However, there are cases where some other developer wanted to combine code that was written using different programming languages into a single assembly. To do this, she would instruct her compilers to compile the code into a module, rather than an assembly. Once that was done, she would generate the assembly manifest that binds the modules together into a logical assembly using the assembly linker tool.
This is best illustrated by an example. In our example, we will create a multi-module assembly that contains both C# and VB.NET code.
Here’s the C# code:
using System;
namespace Module1 { public class AClassInModule1 { public static void SayHello() { Console.WriteLine( “Hello from Module1” ); } } }
To tell the C# compiler to compile this code into a module instead of an assembly, you would have to issue the following command from your command prompt:
csc /t:module module1.cs
Note that the compiler generated a new file called module1.netmodule. The default filename extension for all .NET modules is .netmodule.
Here’s the VB.NET code:
Imports System
Namespace Module2
Public Class AClassInModule2
Public Shared Sub SayHello()
Console.WriteLine( "Hello from VB Module2" )
End Sub
End Class
End Namespace
To tell the VB.NET compiler to compile this code into a module instead of an assembly, you would have to issue the following command from your command prompt:
vbc /t:module module2.vb
The VB.NET compiler generated a new file called module2.netmodule.
To generate the manifest that binds the two modules that we created earlier, we need to use the assembly linker utility (AL.EXE). Here’s the command that generates a DLL that contains the assembly manifest:
al /t:lib /out:master.dll module1.netmodule module2.netmodule
If you look at the manifest that is generated by AL.EXE using ILDASM, you’ll see the following (edited) manifest:
// Deleted extern assembly references
.assembly master
{
// Deleted DebuggableAttribute comment
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.file Module1.netmodule
// Deleted hash
.file Module2.netmodule
// Deleted hash
.class extern public Module1.AClassInModule1
{
.file Module1.netmodule
.class 0x02000002
}
.class extern public Module2.AClassInModule2
{
.file Module2.netmodule
.class 0x02000002
}
.module master.dll
// MVID: {6DBAEEA8-9B41-436A-A7F8-2A4824175652}
.imagebase 0x00400000
.subsystem 0x00000003
.file alignment 512
.corflags 0x00000001
// Image base: 0x02ee0000
The manifest reveals several interesting things:
- The .file declarations identify the two .netmodule files that were created by the csc.exe and vbc.exe compilers.
- All public types must be explicitly exported from those modules using the .class extern declarations.
- The .class extern declarations map the name of the Type to the module that it was implemented in and its TypeDef token value.
- An interesting observation: the two exported types have the same TypeDef token value: 0x02000002. This should not be surprising since TypeDef tokens are scoped by the module that they are defined in and not by the assembly that they are found in.
Why does all of this information exist? To make it possible for the execution engine to resolve type references at runtime. You may not know this, but the runtime always resolves types based on their full names (namespace + name).[1] When a compiler needs to generate code to call a method on one of our exported types, it needs to generate a reference to that type. That reference must contains the referenced type’s full name. At runtime, when the execution engine attempts to locate the referenced type, it needs to consult the master.dll’s manifest to resolve the type name to a module + TypeDef token for the referenced type.
This opens up a potentially nasty can of worms for the developer who created the two modules in the first place. Since the two modules were compiled independently of each other, the possibility of a name conflict exists. Since each compiler has no knowledge of the other module being compiled, it cannot inform her of a name collision at compile time.
You would expect that the assembly linker would at least warn you of an impending name collision, right? Wrong. It silently picks the type from the first module and exports that type only. Be careful!
Enough introductory stuff. In a future entry in this web log, I’ll discuss why I had to create this multi-module assembly in the first place. As it turns out, writing this entry really helped me solidify my understanding of the more arcane details of multi-module assemblies. This is another reason for me to maintain this web log: there is no better way to really understand something than by trying to explain it to somebody else.
As always, please feel free to send comments my way.
[1] Namespaces are a work of fiction; the runtime does not have any knowledge of namespaces, they are just created by the compilers to ease the burden on developers.
5 archived reader comments
Rosireddyr (2003-02-20)
I have one big issue since one week. is .netmodule will have manifest ? I was developed 2 .net modules and combines these .netmodules to the dll. At any time if I check, the .netmodule is having manifest. according to microsoft .netmodule don’t have manifest. only assembly will have manifest. If I see manifest, I didn’t find any difference between .netmodule and assembly anybody can give the explanation. Please contact me at rosireddyr@ctd.hcltech.com thanks Rosi
Jonx (2003-05-09)
is there a way to see from a compiled module or assembly if the code was vb.net code or C# or another langage ?
John Lam (2003-05-09)
If the module was written using VB.NET, there is inevitably a reference to the Microsoft.VisualBasic assembly present. I’m not sure if this is true 100% of the time, but it is in all the VB.NET assemblies that I’ve ever looked at.
Vishwa Rao (2004-01-23)
Hi John,
I am new .NET learner. In your URL I see how files are linked to modules and assemblies. You say that it exports only the first type ( in spite of different namespaces it gets same typedef token).. So how do I ensure the both types are exported to the run time separately? Also I really need to understand the hierarchical link between process, app domain, and assembly. Can you give me some example?
Best Regards Vishwa
Vishwa Rao (2004-01-23)
I am new .NET learner. In your URL I see how files are linked to modules and assemblies. You say that it exports only the first type ( in spite of different namespaces it gets same typedef token).. So how do I ensure the both types are exported to the run time separately? Also I really need to understand the hierarchical link between process, app domain, and assembly. Can you give me some example?
Best Regards Vishwa