Calling Assemblies


Now let's look at what happens when Shapesis referenced by a program.

In the following Try It Out, you'll make a simple client for Shapes named ShapeUser, and take a look at this client in Ildasm before executing it.

Try It Out – Creating a Shapes Client

image from book

To create the client program that uses the Shapes assembly, follow these steps:

  1. Create a new console application project named ShapeUser in the directory C:\BegVCSharp\ Chapter26. (See Figure 26-14.)

    image from book
    Figure 26-14

  2. Rename the Program.cs source file to ShapeUser.cs, and enter the following code:

    #region Using directives using System; using System.Collections.Generic; using System.Text; using Shapes; #endregion     namespace ShapeUser { public class ShapeUser { public static void Main()  { Circle c = new Circle(1.0F); Console.WriteLine("Area of Circle(1.0) is {0}", c.Area()); Console.ReadKey(); // press a key to exit program } } } 

    This project will need a reference to the Shapes project to build correctly, so choose Add Reference... from the Project menu (or right-click on References in the Solution Explorer and select Add Reference...), and the Add Reference dialog will appear. Select the Browse tab in this dialog, then navigate to the Shapes.dll file in the Shapes\bin\Debug directory under C:\BegVCSharp\Chapter26\Shapes (or wherever you have created it), as shown in Figure 26-15.

    image from book
    Figure 26-15

image from book

Click the OK button. Shapes is added to the list of references for the ShapesUser project, as shown in Figure 26-16.

image from book
Figure 26-16

A private copy of Shapes.dll has been added to the ShapeUser bin\Debug directory — I'll discuss this more in the next section.

Build the ShapeUser application.

Now that ShapeUser has been built, start Ildasm again and navigate to the C:\BegVCSharp\Chapter26\ ShapeUser\ShapeUser\bin\Debug directory (or wherever you have created it, again) and open ShapeUser.exe to examine its contents, as shown in Figure 26-17.

image from book
Figure 26-17

Note the private copy of Shapes.dll; I'll explain that in the next section. Click the Open button to proceed. Note that even though this is a stand-alone executable program, it has metadata just like a class library does. Double-click on MANIFEST to see the external reference to the Shapes component, as shown in Figure 26-18.

image from book
Figure 26-18

Finally, to see the assemblies run together, close Ildasm, then execute ShapeUser.exe by pressing F5 to execute in Debug mode. ShapeUser.exe will create a Circle object using the class defined in Shapes.dll, and will call the Area method to get its area. The output appears in a separate Console window, as shown in Figure 26-19.

image from book
Figure 26-19

Press a key to exit the program (or press Shift-F5 to stop debugging), and now you can move on to learn about private and shared assemblies.




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

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