Appendix E. Working with Assemblies

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.

Building Shareable Assemblies

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 shared assembly (using the al utility).

Building Modules

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

Linking Modules to Assemblies

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.dll b.dll

Building with Your New Assemblies

You can now create applications with the new assembly by referencing it on the compiler command line. 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.

Sharing Assemblies

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:

  1. You must generate a key file. Example:

    sn -k key.snk
  2. Then build an assembly that is signed with the strong name:

    al /out:e.dll /keyfile:key.snk a.dll b.dll
  3. Once your assembly is signed, you can install it in the global assembly cache.

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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.254.44