The Assembly Linker


The .NET Framework SDK comes with a useful tool called the Assembly Linker ( al.exe ). This tool can take any number of modules and files, and link them together into a single assembly. Although the C# compiler can perform many of the same functions as the Assembly Linker, the Assembly Linker is useful in mixed-language or non-C# environments.

Unlike the traditional "object file linkers" with which many developers are familiar from their C and C++ experiences, the Assembly Linker does not actually create a single new file with each module or resource embedded inside it. Instead, it creates a new .NET module that contains an assembly manifest referencing all of the modules for the assembly. Like the C# compiler, the Assembly Linker allows resources to be either embedded or linked. Any .NET modules that are part of the assembly can only be linked, however ”there is no equivalent embedding option for modules.

Let's continue the evolution of the example assembly by using the Assembly Linker tool for the next assembly. The C# compiler is still used to create a .NET module from the C# source file, but it will not put an assembly manifest in the newly created module.

The C# source code remains exactly the same for this version of the assembly, but both makefile and message.txt are modified. The complete source code appears in the AboutBoxLinkedAL subdirectory.

The following commands are now used to build the assembly:

 csc /t:module AboutBox.cs ..\AboutBoxBase.cs  al /linkresource:message.txt /out:AboutBox.dll \     \AboutBox.netmodule 

The makefile passes /t:module to the C# compiler to request that the compiler create a module rather than an assembly. By default, C# adds an extension of .netmodule to the modules it creates, yielding AboutBox.netmodule . The Assembly Linker is then invoked, with a request to create the assembly as AboutBox.dll , link to the module AboutBox.netmodule , and link to the resource message.txt .

Let's build and execute this version of the assembly. It should produce the exact same behavior as did the previous incarnation of the assembly; only the tools used to build it have changed. To demonstrate that the module is actually linked to the assembly rather than embedded inside it, simply remove the file AboutBox.netmodule , and the assembly AboutBox.dll will fail to load.

Figure 5.7 depicts the assembly created with this version. The file AboutBox.dll contains the assembly manifest, but nothing else of significance to this discussion. The module and resource files are included as part of the assembly by virtue of the reference in the assembly manifest to the two files, as shown by the solid lines. The implied relationship between the module and the text file is shown by the dashed line.

Figure 5.7. Assembly Linker “ constructed assembly with linked resource

graphics/05fig07.gif



Programming in the .NET Environment
Programming in the .NET Environment
ISBN: 0201770180
EAN: 2147483647
Year: 2002
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net