Chapter 3. Creating Assemblies

This chapter introduces assemblies, which are the .NET unit of deployment, reuse, versioning, and security. Assemblies are a logical rather than a physical structure and include one or more modules and resources. Assemblies are self-describing, containing metadata in a manifest that describes the contents and dependencies of the assembly.

The assembly manifest is the link between the data types contained in an assembly and the common language runtime (CLR). The CLR relies on the metadata in the assembly to load the assembly and required libraries, enforce security policies, handle version support, and perform type validation.

Using the C# Compiler

This section demonstrates how to compile C# source files by using the C# compiler, csc.exe. This is a simple process, similar to using the javac compiler. The significant difference is the result of the compilation process, which we’ll discuss in the Assemblies section later in this chapter.

The csc.exe application can be found in the main directory of the .NET Framework installation, typically C:<windows directory>Microsoft.NETFrameworkv<version number>. As is the case with the other .NET command-line tools, csc.exe will not be added to your PATH environment variable during the installation process, so you’ll need to do it manually.

The following code represents the C# Hello World! application:

class MyApplication  {
    static void Main(string[] args) {
        System.Console.WriteLine("Hello World!");
    }
}

The syntax and keywords of the C# language are described in Chapter 4. For the moment it’s not important to understand the meaning of the C# keywords but simply to accept that this small program will print the Hello World! string when executed.

We save this code in a source file named MyApplication.cs by using a standard text editor; both ASCII and UTF-8 formats are supported. The .cs extension is the standard for C# files. The source file is compiled using the C# compiler (csc.exe) as follows:

csc MyApplication.cs

The compiler produces an executable file named MyApplication.exe that prints Hello World! when executed. There’s no need to invoke the runtime explicitly as you do with Java; Microsoft Windows understands that this is a compiled Microsoft intermediate language (MSIL) application and implicitly invokes the common language runtime.

The general usage form for the C# compiler is

csc [compiler options] [list of source files to compile]

Table 3-1 lists the most important options that can be used with the C# compiler. Note that many of the compiler options have abbreviated forms; see the online documentation for more details.

Table 3-1. The Most Commonly Used C# Compiler Options

Option

Description

Input Files

 

/addmodule:<file list>

Specifies a list of modules to be linked. See the Assemblies section for more information.

/reference:<file list>

Specifies a list of assemblies to be referenced. See Appendix B, for more information on using references.

/recurse:<wildcard>

Searches the current directory and all subdirectories for files to compile that match the specified wildcard.

/nostdlib

Doesn’t reference the standard library (mscorlib.dll).

Output Files

 

/out:<file>

Specifies the name of the output file.

/target:exe

/target:library

/target:module

/target:winexe

Specifies the format of the output file. See the Assemblies section for more information.

/define:<symbol list>

Defines conditional compilation symbols. See Chapter 6, for more information.

/optimize[+|-]

Enable code optimization.

/doc:<file>

Generate XML class documentation to the specified file.

Debugging

 

/bugreport:<file>

Creates a file containing information that can be used to report a bug with the C# compiler.

/checked[+|-]

Specifies whether integer arithmetic overflows are checked. See Chapter 6 for more information.

/debug[+|-]

Specifies whether debugging information should be included in the output.

/debug:full|pdbonly

Specifies the type of debugging information that is generated. The full option allows a debugger to be attached to a running application, and the pdbonly option requires the application to be started by the debugger for MSIL statements to be viewed.

/fullpaths

References to files in the output files will be fully qualified.

/nowarn:<warning list>

Suppresses specified warnings from the compiler.

/warn:<n>

Specifies a warning level (0 to 4).

 

Level 0 disables all warnings.

 

Level 1 displays only severe warnings.

 

Level 2 displays level 1 warnings plus certain less severe warnings, such as warnings about hiding class members.

 

Level 3 displays level 2 warnings plus certain less severe warnings, such as warnings about expressions that always evaluate to true or false.

 

Level 4 displays all level 3 warnings plus informational warnings.

/warnaserror[+|-]

Specifies whether warnings should be promoted to errors.

Resources

 

/linkresource:<resinfo>

Links a .NET managed resource.

/resource:<resinfo>

Embeds a .NET resource in the output file.

/win32icon:<file>

Inserts an .ico file into the output.

/win32res:<file>

Inserts a Win32 resource into the output.

Miscellaneous

 

/main:<type>

Specifies the application entry point. See the Assemblies section for more information.

/lib:<file list>

Specifies a list of directories to search for assemblies specified using the /reference option.

/?

/help

Prints a usage guide that contains a complete listing of all compiler options.

/incremental[+|-]

Enables incremental compilation of source files.

/unsafe[+|-]

Permits compilation of code that contains the unsafe keyword. See Chapter 6 for more information.

..................Content has been hidden....................

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