Building a .NET Code Library with Mono
To illustrate Mono in action, you will begin by building a code library named
CoreLibDumper.dll. This assembly contains a single class type named CoreLibDumper that supports a static method named
DumpTypeToFile(). The method takes a string parameter that represents the fully-qualified name of any type within
mscorlib.dll, and obtains the related type information via reflection, dumping the class member signatures to a local file on the hard drive.
Listing 1 contains the complete code.
The method in
Listing 1 outputs a
.txt file containing the type's details and named according to its fully qualified name. Thus, if the incoming string parameter is System.Threading.Thread, the method creates the output metadata in a local file named
System.Threading.Thread.txt.
While you could compile this file by specifying each required argument manually at the command line, it's much simpler to use a response file. To do so, create a new file named
CoreLibDumper.rsp (in the same location as
CoreLibDumper.cs) that contains the following command set:
/target:library
/out:CoreLibDumper.dll
CoreLibDumper.cs
You can now compile your library at the command line as follows:
gmcs @CoreLibDumper.rsp
This approach is functionally equivalent to the following (more verbose) command set:
gmcs /target:library /out:CoreLibDumper.dll
CoreLibDumper.cs
Giving CoreLibDumper.dll a Strong Name
Mono supports the notion of deploying strongly-named and shared assemblies to the Mono Global Assembly Cache (GAC). To generate the necessary public/private key data, Mono provides the
sn.exe command line utility, which functions more or less identically to Microsoft's tool of the same name. For example, the following command generates a new
*.snk file (specify the
-? option to view all the possible commands):
sn /k myTestKeyPair.snk
To tell the C# compiler to use the key data to assign a strong name to
CoreLibDumper.dll, simply update your
CoreLibDumper.rsp file with the additional command shown in bold text below:
/target:library
/out:CoreLibDumper.dll
/keyfile:myTestKeyPair.snk
CoreLibDumper.cs
 | |
| Figure 3. Assembly Details: You can use monodis.exe to view the CIL code, metadata, and manifest of an assembly. |
Now recompile your assembly:
gmcs @CoreLibDumper.rsp
Viewing the Updated Manifest with Monodis.exe
Before deploying the assembly to the Mono GAC, allow me to introduce the
monodis.exe command-line tool, which is the functional equivalent of Microsoft's
ildasm.exe. Using
monodis.exe you can view the CIL code, manifest and type metadata for a specified assembly. In this case, you want to view the core details of the new (now strongly named) assembly via the
-assembly flag.
Figure 3 shows the result of the following command set:
monodis --assembly CoreLibDumper.dll
 | |
| Figure 4. Mono's Global Assembly Cache: You use the Mono gacutil.exe utility (similar to Microsoft's tool of the same name) to manage assemblies placed into the GAC. |
As you can see, the assembly's manifest now exposes the public key value defined within
myTestKeyPair.snk.
Installing Assemblies into the Mono GAC
Now that you have provided
CoreLibDumper.dll with a strong name, you install it into the GAC using
gacutil.exe. Just like Microsoft's tool of the same name, Mono's
gacutil.exe supports options to install, uninstall, and list the current assemblies installed under the Mono GAC (in
C:\Program Files\Mono-\lib\mono\gac). The following command deploys
CoreLibDumper.dll to the GAC and sets it up as a shared assembly on the machine (be sure to use a Mono command prompt to install this binary to the Mono GAC):
gacutil -i CoreLibDumper.dll
If, after running the command, you open the
\gac directory, you should find a new folder named
CoreLibDumper (see
Figure 4), which defines a subdirectory that follows the same naming conventions as Microsoft's GAC (
versionOfAssembly__publicKeyToken).