Ever wondered how one assembly references types in another assembly? The following is a simplified explanation of what goes on. This is a lab-style entry. If you really want to understand how this happens, follow along using your own computer.
In this example, we’re going to look at System.Xml.Dll using ILDASM. Run the following command from the directory that contains System.Xml.Dll:
ildasm /tokens system.xml.dll
Notice that I used the /tokens command line switch that instructs ILDASM to display token values in its disassembly.
Navigate to the System.Xml.XmlDocument class and find its Load( System.IO.Stream ) method. This is a method that clearly references a type (System.IO.Stream) that is defined inside of another assembly (mscorlib).
When you disassemble the method it by double-clicking on the method name in ILDASM, you should see the following lines at the top of the disassembly window:
.method /*060002DF*/ public hidebysig newslot virtual instance void Load(class [mscorlib/* 23000001 */]System.IO.Stream/* 0100000F */ inStream) cil managed
Metadata in assemblies is identified by tokens. The token values in our disassembly are enclosed inside of the inline comments (/* */).
There are many different types of metadata tokens. Tokens can refer to type definitions, type references, method definitions, method references, and so on. The type of a token can be readily discerned by looking at the value of its most signifant byte.
In our example, there are three types of tokens in the disassembly shown above:
- A MethodDef (method definition) token (0x060002DF)
- An AssemblyRef (assembly reference) token (0x23000001)
- A TypeRef (type reference) token (0x0100000F)
The remaining 24 bits of the token value is either an index into a row in a metadata table (as is the case for all three tokens types shown here) or an offset into a heap (which is the case for all String and UserString tokens).
You can find definitions of all token types in the Partition II - Metadata specification in the %FrameworkSDK%\Tool Developers Guide\docs directory.
The token that we’re going to focus on is the TypeRef token. In our example, this token referes to the System.IO.Stream type in the mscorlib assembly. The question that we want answered is what information is required to find that type at runtime? In other words, what does MSCOREE have to do to find this type?
To see this in action, we need to use a tool that ships as uncompiled source code in the Framework SDK. That tool is METAINFO. Navigate to the %FrameworkSDK%\Tool Developers Guide\Samples\metainfo directory. Build the sample by typing the following command in your command prompt:
nmake
To make your life easier, put METAINFO.EXE on your path by copying Debug\Metainfo.exe to a directory on your path (I like to put it in my \Windows directory).
Next you want to dump the metadata in System.Xml.Dll out to a text file that you can read. Issue the following command from the directory that contains System.Xml.Dll:
metainfo -raw -heaps System.Xml.Dll > out.txt
Open the generated text file, out.txt, using Notepad. The second table that you will see is the TypeRef table. Locate the 0F row in this table. This, of course, corresponds to the row in the TypeRef table that would be referenced by our TypeRef token whose value is 0x0100000F:
f == 0:ResolutionScope[23000001], 1:string#27c, 2:string#283
There are three columns in this row. The first column is the ResolutionScope of this type reference. This tells the execution engine which assembly this type resides in. The next two columns provide offsets into the Strings heap for the type’s name and the type’s namespace. Here are the relevant entries from the Strings heap:
0000027c: Stream
00000283: System.IO
Note that the fact that there is a namespace column in the TypeRef table does not mean that the runtime understands namespaces. This presence of a namespace column only serves to act as a form of data compression in the metadata tables. This is understandable since many types can be referenced from the same namespace, and this scheme avoids duplicating the namespace string unnecessarily in the Strings heap.
Let’s turn our attention to the ResolutionScope token. In this case, the ResolutionScope token happens to be an AssemblyRef token (0x23000001). Here’s the first row from the AssemblyRef table:
35: AssemblyRef cRecs: 2(0x2), cbRec: 28(0x1c), cbTable: 56(0x38)
col 0: MajorVersion oCol: 0, cbCol:2, USHORT
col 1: MinorVersion oCol: 2, cbCol:2, USHORT
col 2: BuildNumber oCol: 4, cbCol:2, USHORT
col 3: RevisionNumber oCol: 6, cbCol:2, USHORT
col 4: Flags oCol: 8, cbCol:4, ULONG
col 5: PublicKeyOrToken oCol: c, cbCol:4, blob
col 6: Name oCol:10, cbCol:4, string
col 7: Locale oCol:14, cbCol:4, string
col 8: HashValue oCol:18, cbCol:4, blob
=================================================
1 == 0:0001, 1:0000, 2:0ce4, 3:0000, 4:00000000, 5:blob#14, 6:string#7, 7:string#0, 8:blob#0
Notice here that I also printed out the legend that is supplied by MetaInfo to simplify our discussion.
All of the information found in this table is used to locate the assembly that is referenced. In this case, these are what their values are:
- MajorVersion = 1
- Minor Version = 0
- Build Number = 3705
- Revision Number = 0
- Flags = 0
- PublicKeyOrToken = b7 7a 5c 56 19 34 e0 89
- Name = mscorlib
- Locale = NULL
- Hash Value = NULL
This information is passed by MSCOREE to a mysterious piece of OS software called Fusion. It lives inside fusion.dll. Fusion’s job is to use this information together with a bewildering array of policy information to locate the correct assembly on your computer. The details are described in the Framework SDK documentation in the How the Runtime Locates Assemblies and Assembly Versioning articles.
Unfortunately for guys like me, there isn’t a documented API where I can pass this information and get back a path to the correct assembly. This limitation makes it virtually impossible (unless I want to duplicate the functionality of fusion.dll myself - unlikely) for me to correctly resolve assembly references at runtime. There are hacks that I am using to work around this problem, but none of them make me particularly happy.
Hopefully this has helped to clarify some of the questions that y’all might have about how type references are resolved in the CLR. Please send comments to the usual place.