Assemblies and Manifests


Packaging a component so that other programs can use it presents a number of challenges. Traditionally, a COM component is packaged in a dynamically linked library. When client programs seek to access a component, the operating system loads the component's DLL into the client's address space. The client can then utilize the functionality of the component. Although this system offers benefits, such as code reuse and efficiency, it creates a number of problems as well. For instance, the loading of DLLs is normally based on the name of the DLL. With versioning of the components , the name of the DLL does not necessarily change but the version of the component does. Thus, clients may get incompatible versions of the DLL loaded at different times, as DLLs are often replaced during software upgrades. This introduction of incompatible upgrades of component DLLs is often referred to as DLL hell.

To address these issues, the CLR packages types in an assembly. To use a simple analogy, an assembly could be described as a super-DLL. That is, an assembly remains the executable housing for a component, but it also provides facilities such as version information and type resolution (which traditional DLLs do not). Assemblies are covered in more detail in Chapter 5, on building applications, but a simple overview is provided here because they are such a fundamental building block in the CLR.

From a client's viewpoint, an assembly is a collection of publicly available types and resources that developers use in their programming. From a developer's viewpoint, it is a collection of files that form a functional unit. These files can contain types and resources that are either publicly available to other assemblies or an internal implementation detail. In other words, an assembly represents a means of grouping related types. It is also the unit of deployment and versioning, as all types within an assembly are part of the same version.

Figure 3.6 shows the logical elements of a simple assembly, called Assembly A in the diagram. An assembly comprises one or more modules, one of which has a manifest. It is the addition of a manifest to a module that creates the assembly. The manifest contains information about the assembly, such as its name, its version information, and the names of all modules and files that form the assembly. A module is a file that contains CLR metadata. Also shown in Figure 3.6 is a non-CLR file, called File A here. Because it is not a module, this file would not contain any CLR metadata but it might hold resources used by the assembly's type, such as bitmaps.

Figure 3.6. A simple assembly

graphics/03fig06.gif

Like .NET types, assemblies are self-describing . Information about an assembly appears in its manifest. A manifest contains the following data:

  • Name, originator, and version information

  • Configuration and culture information

  • A list of types and their locations in the assembly

An assembly always has a simple name, but it may also have a strong name. A strong name utilizes public and private keys to ensure the security of the assembly; no one can produce an assembly with the same strong name without knowing the keys for the publisher that produced the assembly. If a strong name is provided, the manifest will contain a public key token that is based on the public key for the publisher as well as information identifying the hashing algorithm used.

The version number consists of four parts :

  • Major version

  • Minor version

  • Build number

  • Revision number

The version number can be used to differentiate between versions of the assembly. Thus, two assemblies with the same name and publisher will be considered different if they vary in terms of their version numbers . The execution engine permits the loading of different versions of assemblies into an executing program.

To help clarify the information contained in an assembly's manifest, Listing 3.8 demonstrates how developers access aspects of an assembly. This program uses the assembly mscorlib , which contains the Object and String classes along with other types, and displays information about the assembly, such as its full name, version, and location. Next, the program displays the modules and files that make up the assembly.

Listing 3.8 Using an assembly
 using System; using System.Reflection; using System.IO; namespace AssemblyInformation {   class Sample   {     static int Main(string[] args)     {       Type t= Type.GetType("System.String");       Assembly a = Assembly.GetAssembly(t);       AssemblyName an = a.GetName(false);       Console.WriteLine("AssemblyName: {0}", an.Name);       Console.WriteLine("FullName: {0}", an.FullName);       Console.WriteLine("Codebase: {0}", an.CodeBase);       Console.WriteLine("Version: {0}", an.Version);       Console.WriteLine("Location: {0}", a.Location);       Module[] modules = a.GetModules();       Console.WriteLine("Module Name(s):");       foreach(Module m in modules)       {         Console.WriteLine("\t FullName: {0}",                               m.FullyQualifiedName);         Console.WriteLine("\t ScopeName: {0}",                               m.ScopeName);         Type[] types = m.GetTypes();         Console.WriteLine("Module Types:");         foreach(Type tn in types)           Console.WriteLine("\t\t {0}", tn);       }       FileStream[] files = a.GetFiles(true);       Console.WriteLine("File Name(s):");       foreach(FileStream f in files)       {         Console.WriteLine("\t {0}", f.Name);       }       String[] rn = a.GetManifestResourceNames();       Console.WriteLine("Resource Name(s):");       foreach(String s in rn)       {         Console.WriteLine("\t {0}", s);       }       return 0;     }   } } 

The program first retrieves the String class's Type object and then uses this Type object to access its assembly information. It displays information about the assembly, such as its name, code base, version, and location on disk. The program iterates through each module, displaying module identification information as well as types from the module. Finally, it iterates through the files contained within the assembly and then through all resources.

Although the output generated by Listing 3.8 on your system may vary from that shown below because of differences in versions of the CLR, the structure of the output will remain the same. The number of types displayed is enormous , so the following output has been edited; ellipses ( ) indicate parts of the output where editing has occurred.

 AssemblyName: mscorlib  FullName: mscorlib, Version=1.0.3300.0, Culture=neutral,   PublicKeyToken=b77a5c561934e089 Codebase: file:///c:/windows/microsoft.net/framework/   v1.0.3705/mscorlib.dll Version: 1.0.3300.0 Location: c:\windows\microsoft.net\framework\v1.0.3705\mscorlib.dll Module Name(s):     FullName: c:\windows\microsoft.net\framework\v1.0.3705\mscorlib.dll     ScopeName: CommonLanguageRuntimeLibrary Module Types:     System.Object     System.ICloneable     System.Collections.IEnumerable     System.Collections.ICollection     System.Collections.IList     System.Array     ... File Name(s):    c:\windows\microsoft.net\framework\v1.0.3705\mscorlib.dll    c:\windows\microsoft.net\framework\v1.0.3705\prc.nlp    c:\windows\microsoft.net\framework\v1.0.3705\prcp.nlp    c:\windows\microsoft.net\framework\v1.0.3705\ksc.nlp    c:\windows\microsoft.net\framework\v1.0.3705\ctype.nlp    c:\windows\microsoft.net\framework\v1.0.3705\xjis.nlp    c:\windows\microsoft.net\framework\v1.0.3705\bopomofo.nlp    c:\windows\microsoft.net\framework\v1.0.3705\culture.nlp    c:\windows\microsoft.net\framework\v1.0.3705\region.nlp    c:\windows\microsoft.net\framework\v1.0.3705\sortkey.nlp    c:\windows\microsoft.net\framework\v1.0.3705\charinfo.nlp    c:\windows\microsoft.net\framework\v1.0.3705\big5.nlp    c:\windows\microsoft.net\framework\v1.0.3705\sorttbls.nlp    c:\windows\microsoft.net\framework\v1.0.3705\l_intl.nlp    c:\windows\microsoft.net\framework\v1.0.3705\l_except.nlp Resource Name(s):    mscorlib.resources    prc.nlp    prcp.nlp    ksc.nlp    ctype.nlp    xjis.nlp    bopomofo.nlp    culture.nlp    region.nlp    sortkey.nlp    charinfo.nlp    big5.nlp    sorttbls.nlp    l_intl.nlp    l_except.nlp      ... 

Chapter 4, on the execution system, discusses assemblies and execution system functioning in more detail. Also, Chapter 6, on application deployment, provides more information about the creation of assemblies.



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