Example: A Simple Assembly


To explain the assembly concepts, this chapter includes a progressive example that demonstrates the development of an AboutBox component as an assembly. The shared AboutBox component in this assembly can be reused by any number of applications. As the example progresses, it highlights the use of embedded and linked resources, different versions of assemblies living side by side in the .NET environment, and several other key concepts.

The code for this example has been split up among many directories, with each directory containing a fully functional code segment. The full source code for each example is not shown in this book, but rather only the relevant changes required to demonstrate the concepts under discussion. Each code segment does indicate the directory containing the complete source code for that particular stage of the assembly.

The example assembly makes use of a base class AboutBoxBase (implemented in AboutBoxBase.cs ), which provides much of the GUI logic needed for the dialog. As development of the example progresses, new subclasses are created from this base class, but the base class itself is never modified. As the purpose of this example is to demonstrate assembly concepts, this base class is not shown, but it can be found with the example code segments. The base class expects the programmer to provide a single overridden method, GetAboutText() , to return the text for the dialog.

The assembly example also uses a client program called UseAboutBox.exe . As with AboutBoxBase.cs , the source code in UseAboutBox.cs does not change during the development of the progressive example. For each version of the example, the client application is simply recompiled, referencing the specific version of AboutBox code it uses.

Version 1 of AboutBox

The first version of the AboutBox example uses a hard-coded string in the text of the dialog. Listing 5.1 provides the code for this version. As you can see, this trivial example defines a single namespace ( AboutBoxSample ), a single class ( AboutBox ) that derives from the base class ( AboutBoxBase ), and one method ( GetAboutText() ) that returns the hard-coded string. This file is found in the AboutBoxSample directory of the samples.

Listing 5.1 AboutBox: Version 1
 namespace AboutBoxSample {   public class AboutBox : AboutBoxBase     {         public override string GetAboutText()         {             return "Simple AboutBox";         }   } } 

Building the Assembly with nmake and makefile

As this chapter progresses, the instructions for building the examples become more complex and more vulnerable to errors. For this reason, the assembly example uses the Microsoft nmake utility, and makefile files are provided with each code sample.

nmake is Microsoft's version of the UNIX make tool, and a version is included with the .NET platform SDK. nmake processes a makefile , which indicates how to build each target (often an output file) and lists dependencies between files involved in the build process. For example, a makefile might indicate that the target AboutBox.dll depends on AboutBox.cs (meaning that any changes to AboutBox.cs should cause AboutBox.dll to be rebuilt) and that a specific command can be used to create AboutBox.dll .

The makefile for version 1 of the assembly example follows . Although a complete discussion of nmake and makefile is beyond the scope of this book, a quick description of these tools should help you to understand the intention behind their use. The first line in the following code indicates that to build the first target in the makefile , all , you need to build two other targets, AboutBox.dll and UseAboutBox.exe . The next two lines indicate that AboutBox.dll should be rebuilt whenever AboutBox.cs or ..\AboutBoxBase.cs changes; to build this DLL, the C# ( csc ) command specified in the makefile should be executed. Next, the code indicates that UseAboutBox.exe depends on UseAboutBox.cs from the parent directory, and the C# command line is given. Finally, the code identifies a target called clean ; like the target all , it does not identify an output file but rather indicates how to clean up after building so that only the source files remain .

 all: AboutBox.dll UseAboutBox.exe  AboutBox.dll: AboutBox.cs ..\AboutBoxBase.cs   csc /t:library AboutBox.cs ..\AboutBoxBase.cs UseAboutBox.exe: ..\UseAboutBox.cs   csc /t:winexe /r:AboutBox.dll /out:UseAboutBox.exe \       ..\UseAboutBox.cs clean:     -del *.exe     -del *.dll 

To build this sample, simply change to the sample directory, and execute nmake . You should see a number of commands executed as each target is built, and a directory listing should include AboutBox.dll and UseAboutBox.exe .

After building this example, you can execute it by running UseAboutBox.exe , and selecting About from the Help menu. See Figure 5.1

Figure 5.1. First version of UseAboutBox.exe in action

graphics/05fig01.gif

Functioning of the makefile

You may be wondering: What exactly does the makefile instruct the C# compiler to do?

The command line used to build version 1 of the assembly (from the makefile ) follows:

 csc /t:library AboutBox.cs ..\AboutBoxBase.cs 

The code passes two source file names : the source file for version 1 of the assembly and the base class that is shared among all versions of the assembly. The /t:library option directs C# to build an assembly from the two source files. When C# creates an assembly, it places the assembly manifest in the single output file created by the compiler; thus a single AboutBox.dll is created that contains both the module with the compiled code and the assembly manifest.

Figure 5.2 shows this simple assembly. It includes a single assembly ( AboutBox ), which consists of a single file ( AboutBox.dll ). This file contains both the module code and the assembly manifest.

Figure 5.2. Construction of version 1 of the assembly

graphics/05fig02.gif

UseAboutBox.exe is the example application that uses this assembly. The following command line is used to build this application:

 csc /t:winexe /r:AboutBox.dll /out:UseAboutBox.exe \      ..\UseAboutBox.cs 

Note that only one source file is used to build this application, ..\UseAboutBox.cs . As this code is reused for all subsequent versions of the assembly, it resides in the parent directory.

Version 1 of the assembly also passes three options to the compiler:

  • /t:winexe instructs C# to create a GUI executable (rather than a library, module, or console executable).

  • /r:AboutBox.dll informs C# that the code references types defined in the AboutBox.dll assembly file. The compiler will load this DLL to resolve type references during the compilation step, and it will also embed information about this assembly in the final executable. When the executable is run, the .NET runtime environment will locate the reference to the assembly and load it accordingly .

  • /out:UseAboutBox.exe specifies to the compiler the file name to create for the final executable.



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