The seventeenth part of the C# Object-Oriented Programming tutorial considers .NET assemblies. The building blocks of .NET programs, assemblies are the files that contain all of the code, versioning information and resources for an application.
What is an Assembly?
Assemblies are an important concept in .NET programming. An assembly is a file that contains a program in its own right or a library of code, controls, resources or other information that can be utilised by a program. Generally speaking, an assembly is created as a single project within Visual Studio, or a similar development environment, and compiled into an executable (EXE) file or a dynamic linked library (DLL).
Assemblies should not be confused with namespaces. Although in many cases a single assembly will contain one namespace, it is possible for a namespace to be distributed throughout many compiled DLLs. Similarly, one assembly may contain any number of namespaces.
Assemblies store four types of information. These are:
- The Assembly Manifest
- Type Metadata
- Microsoft Intermediate Language (MSIL) Code
An assembly's manifest contains information that describes the assembly. The following key items are held:
- Assembly Name. A simple text string that holds the name of the assembly. This is combined with the assembly's version number, culture and strong name to generate the assembly's identity.
- Version Number. The version number is made up of major and minor version numbers and revision and build numbers. This information is used by the .NET framework to ensure that an appropriate version of the assembly is loaded when executing software.
- Culture. The culture information for the assembly. This setting is used for globalised applications to determine the language or locale that the assembly supports. Culture information is used by satellite assemblies. These are special files containing location-specific details, such as translation of text strings to other languages. Satellite assemblies are loaded at run-time when available for the user's preferred culture. Their use is beyond the scope of this article.
- Strong Name. Contains the public key from the software publisher when a strong name has been assigned. Signed assemblies are protected by a digital signature so that the .NET framework can prevent the execution of assemblies that have been modified after compilation. This reduces the risk that viruses and other malicious software could tamper with the contents of the files.
- File List. Simple assemblies usually contain only a single file. However, you can create multiple file assemblies. In either case, the list of files within the assembly is hashed and held in this list.
- Type Reference Information. Holds information used at run-time that maps a type reference to the file that contains the type declaration and code.
- Referenced Assembly Information. Provides a list of other assemblies that are referenced by the current assembly. Each reference contains the first four items from this list, ie. the assembly identity.
- Information Attributes. Additional information related to the assembly. These settings include data such as company details for the software vendor, copyright information and product and trademark details. This information is usually configured in the
The type metadata information held within an assembly contains details of all of the data types and members that are provided by the assembly. This allows the assembly to describe the compiled code that it contains. This is particularly useful when creating software that uses multiple .NET languages as each language can examine the type metadata and automatically understand the data types that are used. No custom language interoperability code needs to be written in this situation.
Microsoft Intermediate Language Code
The third element of an assembly is the program code. The C# classes that you develop are compiled into the Microsoft Intermediate Language (MSIL). This language is of a lower level than C# but is not fully compiled into machine instructions. The final compilation only occurs at run-time, giving the prospect that the MSIL code can be executed under the .NET Common Language Runtime (CLR) on various platforms including 32-bit and 64-bit Windows and, in some cases, non-Windows operating systems.
A second advantage of MSIL is that the code in an assembly can be utilised by any .NET language. This permits modules developed in C# to be used by Visual Basic and C++ developers for example.
There are disadvantages to the two-part compilation of .NET assemblies. The primary concern for many developers is performance. The additional run-time compilation does cause a noticeable pause when a large program starts and execution speed can be lower than with native code. For developers of packaged software, an additional problem of code security is introduced as code that is not protected can be "decompiled" to the original C# source code. This is alleviated somewhat by the use of code obfuscators. However, these are beyond the scope of this article. Thirdly, the use of MSIL requires that the appropriate version of the .NET framework be installed on all target systems.
28 February 2008