Building and Using Assemblies


There are several ways to go about creating an assembly. The first method, using Visual Studio, you should already be quite familiar with. Every time you have created a solution so far in this book, you have created an assembly. When you create a "Class Library" project within Visual Studio, the output of that Class Library is an assembly with the .DLL extension.

The other way to create an assembly is to use the Assembly Linker tool, AL.EXE. This tool provides a command-line interface to linking multiple compiled modules into an assembly. In fact, this is the only way for you to create a multimodule assembly in C#, as opposed to VB.NET, which allows for the creation of multiple modules per assembly directly within Visual Studio.

You can also use; Reflection to obtain information about an assembly. Listing 12.1 shows how you can retrieve some basic information from the assembly's manifest using the Assembly class.

Listing 12.1. Using the Assembly Class

using System; using System.Reflection; using System.Resources; using System.Collections.Generic; using System.Text; namespace AssemblyDemo1 { class Program { static void Main(string[] args) {     Assembly a = Assembly.GetExecutingAssembly();     Console.WriteLine("Current Assembly {0} loaded from {1}", a.GetName().Name, a.Location);     Console.WriteLine("\nLoaded from the Global Assembly Cache? {0}", a.GlobalAssemblyCache);     Console.WriteLine("This Assembly requires version {0} of the runtime",  a.ImageRuntimeVersion) ;     Console.WriteLine("Assembly's entry point {0}.", a.EntryPoint.ToString());     Console.WriteLine("Types contained in this Assembly:");     foreach (Type t in a.GetTypes())     {         Console.WriteLine("\t{0} contained in {1}", t.Name, t.Assembly.GetName().Name);     }     Console.ReadLine(); } } } 

You can also use the Assembly class to load and obtain information from other assemblies. If you add a "Class Library" project to the preceding sample, you can modify the code so it reads information from the compiled DLL in addition to the executable file. This is illustrated in Listing 12.2.

Listing 12.2. Loading an External Assembly

using System; using System.Reflection; using System.Resources; using System.Collections.Generic; using System.Text; namespace AssemblyDemo1 { class Program { static void Main(string[] args) {     Assembly a = Assembly.GetExecutingAssembly();     Console.WriteLine("Current Assembly {0} loaded from {1}", a.GetName().Name, a.Location);     Console.WriteLine("\nLoaded from the Global Assembly Cache? {0}", a.GlobalAssemblyCache);     Console.WriteLine("This Assembly requires version {0} of the runtime", a.ImageRuntimeVersion);     Assembly otherAssembly = Assembly.LoadFile(       AppDomain.CurrentDomain.BaseDirectory + "\\SampleAssembly.DLL");     Console.WriteLine("Other Assembly: {0}", otherAssembly.GetName().Name);     Console.WriteLine("Types contained in {0}", otherAssembly.GetName().Name);     foreach (Type t in otherAssembly.GetTypes())     {         Console.WriteLine(t.Name) ;     }     Console.ReadLine(); } } } 

When you run this application, after having created an empty class library called SampleAssembly and adding a reference to that project from the main project, you get the following output:

Current Assembly AssemblyDemo1 loaded from C:\Documents and Settings\Kevin\My Do cuments\Writing\SAMS\C# 2005 Unleashed\12\Code\AssemblyDemo1\AssemblyDemo1\bin\D ebug\AssemblyDemo1.exe Loaded from the Global Assembly Cache? False This Assembly requires version v2.0.50727 of the runtime Other Assembly: SampleAssembly Types contained in SampleAssembly Class1 




Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net