This appendix describes a number of useful techniques for working with assemblies. Among the topics covered are how to create modules, how to manage the global assembly cache, how to make assemblies shareable, and how to use the nmake utility to automate your builds.
For most applications you build components that are either standalone assembly EXEs or DLLs. If you want to build shareable components that are shared across multiple applications, you need to give your assembly a strong name (using the sn utility) and install it into the global cache (using the gacutil utility).
The al utility doesn’t rebuild assemblies from other assemblies, so you need to compile your C# files as modules, using the flag (/target:module). Here’s an example:
csc /target:module a.cs
Once your modules are built, you can create a single shared assembly with the al (alink) utility. You specify the name of the new assembly and the source modules. Here’s an example:
al /out:c.dll a.netmodule b.netmodule
You can now create applications with the new assembly by referencing it on the compiler command line. For example:
csc /r:c.dll d.cs
Note that in order to use classes within the namespaces
contained in the c.dll assembly of the preceding
example, you need to specify the using
keyword to reference the
namespace.
To share assemblies across multiple applications where the assembly itself doesn’t reside in the same directory, you need to install it into the assembly cache. To install an assembly into the cache, it must be signed with a strong name. The sequence is:
Note that installing an assembly to the shared cache doesn’t copy it to anywhere, so when specifying the reference on compilations, you need to specify the assembly file location for the compiler:
csc /r:..e.dll f.cs
There are two ways to work with the assembly cache. One is with the Windows Explorer, which has a shell extension that displays the cache and allows you to manipulate the entries. If you explore the %SYSTEMROOT%assembly directory, you can display the current cache:
start %SYSTEMROOT%assembly
Alternatively you can use the gacutil utility, which allows you to install, uninstall, and list the contents of the global assembly cache. The following:
gacutil /i e.dll
installs e.dll.
This example:
gacutil /u e
uninstalls all assemblies with the name e
.
This example:
gacutil /u e,ver=0.0.0.0
uninstalls only the assemblye
that matches the version number.
You can use nmake to automate many tasks that build assemblies and modules.
Here’s an example that shows many of the previous command lines in a
cohesive manner. Particular nmake features to note
are the use of the.SUFFIXES
keyword to add a definition for the
.cs file extension and the use of the response file
with the C# compiler, for when more source file names are supplied than
can be listed on the
command line.
REF=/r:c.dll DEBUG=/debug .SUFFIXES: .exe .dll .cs .netmodule .cs.netmodule: csc /t:module $*.cs .cs.exe: csc $(DEBUG) $(REF) @<<big.tmp $*.cs $(SRCLIST) << all : d.exe f.exe d.exe : d.cs c.dll c.dll : a.netmodule b.netmodule al /out:c.dll a.netmodule b.netmodule b.netmodule : b.cs a.netmodule : a.cs key.snk : sn -k $*.snk e.dll : a.netmodule b.netmodule key.snk al /out:$*.dll /keyfile:key.snk a.netmodule b.netmodule gacutil /i $*.dll f.exe : f.cs e.dll csc $(DEBUG) /r:.e.dll f.cs clean: gacutil /u e del key.snk *.netmodule c.dll d.exe e.dll f.exe *.pdb /q