Creating Assemblies

There are many ways that you can create assemblies. If you are one of the many programmers who prefer a command-line over point-and-click, you can simply open a command prompt and make use of the AL.EXE command-line utility. This is the Assembly Linker tool, and is used to link precompiled modules and resources into assemblies.

If you are creating a dynamic assembly, you will probably be using reflection to emit the assembly directly into memory. There are some situations in which this type of code is necessary, but it is by no means the most common way of creating assemblies.

Most developers will be creating assemblies with Visual Studio .NET (or another IDE if you don't use Visual Studio .NET). You create an assembly whenever you build a project that creates either a .DLL or an .EXE output file.

The following section will illustrate how to create a new assembly. It won't do much to start with, but you'll add to it later in the chapter. To start with, open Visual Studio .NET and create a new C# Class Library project. Call the project AssemblyIntro.


A useful habit that many have developed is to delete the Class1.cs file that comes with a new Class Library project. This is done because most developers never use the default namespace. The routine is this: Delete Class1.cs, set the default namespace and assembly name, and then add your new class. The new class will automatically have the correct namespace.

Open the Project Properties dialog and set the default namespace to SAMS.CSharpUnleashed.Chapter12.AssemblyIntro. Make sure that you also set the assembly name to the same value. It will create an assembly called SAMS.CSharpUnleashed.Chapter12.AssemblyIntro.dll. It looks like a long namespace name, but many developers like descriptive namespaces, and there will be a lot of work with namespaces later in the chapter.

Double-click the AssemblyInfo.cs file and fill in the blanks however you like. Set the assembly description to whatever you like. When you're done filling that in, add a new class to the project and call it AssemblyTool.

Set the code for the AssemblyTool class to the code contained in Listing 12.1.

Listing 12.1. The Code for the AssemblyTool Class
 using System; using System.Reflection; using System.Text; namespace SAMS.CSharpUnleashed.Chapter12.AssemblyIntro {   /// <summary>   /// This is the AssemblyTool sample class   /// </summary>   public class AssemblyTool   {     public AssemblyTool()     {     }     public static string GetAssemblyInfo()     {       // gets the Assembly in which this code is executing, not       // necessarily the Assembly of the main executable (EXE)       Assembly thisAssembly = Assembly.GetExecutingAssembly();       AssemblyName thisName = thisAssembly.GetName();       StringBuilder sb = new StringBuilder();       sb.AppendFormat("Assembly Name: {0}\n", thisAssembly.FullName);       sb.AppendFormat("Assembly Version: {0}\n", thisName.Version.ToString());       sb.AppendFormat("Assembly Culture: {0}\n", thisName.CultureInfo.ToString());       return sb.ToString();     }   } } 

The preceding code uses some reflection methods to get access to the current assembly. The Assembly class exposes a lot of methods for obtaining information about any assembly, whether the assembly was loaded from disk, from a URL, by execution, or by being dynamically created in memory.

So, we have an assembly and a class inside that assembly. Let's take a look at it in ILDASM; doing so will be a good exercise in learning what actions in Visual Studio .NET look like as results in an assembly manifest.

Figure 12.3 shows the new assembly manifest from the newly created SAMS.CSharpUnleashed.Chapter12.AssemblyIntro.dll.

Figure 12.3. Assembly manifest from SAMS.CSharpUnleashed.Chapter12.AssemblyIntro.dll.

To test this code and see what the output looks like, add a new Console Application project to the AssemblyIntro solution called Harness. Add a reference from the Harness project to the AssemblyIntro project. Now define the Class1.cs class as shown in Listing 12.2.

Listing 12.2. The Test Harness Console Application
 using System; using SAMS.CSharpUnleashed.Chapter12.AssemblyIntro; namespace Harness {   /// <summary>   /// Summary description for Class1.   /// </summary>   class Class1   {     /// <summary>     /// The main entry point for the application.     /// </summary>     [STAThread]     static void Main(string[] args)     {       Console.WriteLine( AssemblyTool.GetAssemblyInfo() );       Console.ReadLine();     }   } } 

When you run this application, you'll get a console window with the following output:

 Assembly Name: SAMS.CSharpUnleashed.Chapter12.AssemblyIntro, Version=,     Culture=neutral, PublicKeyToken=null Assembly Version: Assembly Culture: 

You should now have a fairly good idea of what the components of an assembly are. You've seen what assembly manifests look like, how they're structured and used by the Common Language Runtime, and the previous exercise showed you what it looks like to create and make use of a single-file assembly.

Before you go on, remember that assemblies are a logical unit of deployment, security, and functionality. Security can be specified using an assembly as a target. Assemblies are the unit of deployment for the .NET Framework, and code, content, and resources can be either embedded in or linked with an assembly. Security is a topic for another chapter, but you'll get an overview of resources and embedded content next.

    Visual C#. NET 2003 Unleashed
    Visual C#. NET 2003 Unleashed
    ISBN: 672326760
    EAN: N/A
    Year: 2003
    Pages: 316 © 2008-2017.
    If you may any questions please contact us: