|
Features of Assembly
The assembly contains MSIL code (from the compilation of your code), resources, and the metadata (which is data that describes the assembly). The metadata defines the following, among other things:
Type information for the methods and public members in the assembly
The name of the assembly
Version information
Strong-name information
Culture that defines the client’s language and cultural preferences
A list of the files in the assembly
A list of referenced assemblies
The assembly is the file that is stored on the disk. As part of that file the metadata defines all the aspects of the assembly and the manifest is the part of the metadata that defines the assembly and its properties. The PE (Portable Executable) file contains metadata, as do all assemblies.
Working with a .NET Assembly
The tool used to view the content of an assembly is the MSIL Disassembler (Microsoft Intermediate Language) and to examine any managed module, including any *.exe, *.dll, and *.netmodule. The syntax for running the MSIL Disassembler is as follows:
ildasm [options] filename [options]
To see a short version of the documentation for the ildasm utility use the /? option as in this example:

There are many types of files that are involved when building .NET assemblies:
Source code resides in files with the file extension .cs.
Managed modules are the compiled IL versions of the source code. The extension of the file that is built when making a module from source files is .net module.
Assemblies are either DLLs or .exe files containing managed modules, resources, and metadata.
Creating and Modifying a .NET Assembly
An assembly can be built using the tools supplied with Visual Studio .NET or with the language compilers (such as csc.exe) and linkers (such as al.exe) supplied with the .NET Framework. We will use the command-line tools in this chapter to build the assemblies.
When we build assemblies, they are built either as private or strongly named assemblies (strongly named assemblies are needed for shared deployment). The simplest assembly to build is the private assembly; all the files for the private assembly are deployed in the folder for the application. Private assemblies are not shared with other applications and are not added to the GAC.
The examples in will work with the following source files that will print "Hello World!" on the console for us:
Hello.cs
namespace HelloWorld
{>br>
using System;
public class Hello
{
string StrHello = "Hello World!";
public Hello()
{
//empty constructor
}
public void SayHello()
{
Console.WriteLine (strHello)
}
}
}
The following is the welcome.cs source file:
welcome.cs
using system;
using Helloworld;
namespace Helloassembly
{
Public class welcome
{
Public Static Void Main()
{
Hello Welcome;
welcome = new Hello;
Welcome.SayHello();
}
}
}
Setting the /target: module compiler command-line switch will produce a module that can be used to create a multi-file assembly. The following example builds the Hello.netmodule file:
csc /target:module Hello.cs
To reference another module to be included in the assembly use the /addmodule switch to make the type information available:
csc /addmodule:Hello.netmodule /t:module Welcome.cs
Once this command has been run, the Welcome.netmodule will be created. To finally link the modules together, you use the al.exe utility (the Assembly Linker), as in this example:
al Welcome.netmodule Hello.netmodule /main:WelcomeAssembly.Welcome.Main
/t:exe /out:Hello.exe
This command produces the Hello.exe output file from the two modules. The /main option tells the linker where to find the Main method for the application. Note that the options are case-sensitive.
Strongly Named Assemblies and GAC
In order to be able to install an assembly in the GAC, it must be a strongly named assembly. The following steps are involved in creating a strongly named assembly:
1. Create a strong-name key file using the Strong Name tool (sn.exe) as in this example:
2. Add assembly attributes to one of the source files referencing the strong-name key file. The following example adds the AssemblyKeyFile and AssemblyVersion attributes to the Hello.cs source file.
3. Compile the library using the csc compiler. Use the /t:library switch to create a DLL, as in this command: csc /t:library Hello.cs
4. Install the module in the GAC using the gacutil.exe utility, as is shown here:
gacutil -i Hello.dll Microsoft (R) .NET Global Assembly Cache Utility. Version 1.0.3705.0
sn -k Hellokey.snk
using system.reflection;
[assembly: AssemblyKeyFile("WelcomeKey.snk")]
[assembly: AssemblyVersion("12.1.42.0")]
namespace Helloworld
{
using system;
public class Hello
{
string strHello = "Hello World!";
public Hello()
{
//empty constructor
}
public void SayHello()
{
Console.WriteLine(strHello);
}
}
}
Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.
Assembly successfully added to the cache
The -i switch installs the assembly, -l lists all the installed assemblies in the GAC, and -u uninstalls an assembly.
Creating and Implementing Satellite Assemblies
When you wish to localize the application (make the application customizable for different languages and cultures), it can be written in culture-neutral code (code that uses resources from an assembly rather than hard-coding the resources in the program) and distribute the localized modules in separate assemblies called satellite assemblies. The culture is made part of the assembly’s identity, as is the version. When the application searches for the proper assembly to load, that information will be used as the basis for the assembly-binding selection performed by the binding manager. The binding selection is performed at run time when the client’s locale and culture is known - the selection will pick the proper assembly (or best fit).
The object model for the assembly is the hub-and-spoke model where the main assembly (the culture- neutral or default assembly) is the hub and the culture-specific satellite assemblies are the spokes. This means you can deploy the application with the default culture first, and then incrementally add cultures. The locale is a setting that is defined on the client computer and specifies a culture that indicates the language that is spoken (English, for example) and a subculture that identifies the country the client is in (such as the United States). The locale defines the measurement system (metric or imperial) and number and date-formatting rules.
When creating the resources that will make up the satellite assembly, you should work with the naming convention suggested by Microsoft. It uses a culture/subculture string to indicate the locale; for example, for the English culture in Canada, the identifier would be en-CA, while the English culture in the United States would be en-US. When creating the resource files, the filename will be in this format: For example, a resource file with strings specific to the English culture might have any of the following names, depending on whether the resource is
English-language neutral or specific to a culture:
<resource_name>.<culture_identifier>,resource
strings.en.resource
strings.en-JM.resource
strings.en-US.resource
To create the satellite assemblies, you will use the al.exe utility (Assembly Linker) to link your resource file to an assembly, as shown in this example:
al /t:library /embed:strings.en.resources /culture:en /out:MyApp.resources.dll
The /t option will direct al to build a library, and the /embed option identifies the file to be linked.
|