Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 10 NAMESPACES AND ASSEMBLIES

Referencing Other Assemblies

In Chapter 1, we took a high-level look at the compilation process. You saw that the compiler takes the source code file and produces an output file called an assembly. This chapter takes a closer look at assemblies and how they are produced and deployed. You will also look at how namespaces help organize types.

All the programs you’ve seen so far have, for the most part, declared and used their own classes. In many projects, however, you will want to use classes or types from other assemblies. These other assemblies might come from the BCL or a third-party vendor, or you might have created them yourself. These are called class libraries, and the names of their assembly files generally end with the .dll extension rather than the .exe extension.

Suppose, for example, that you want to create a class library that contains classes and types that can be used by other assemblies. The source code for a simple library is shown in the following example and is contained in a file called SuperLib.cs. The library contains a single public class called SquareWidget.

Figure 10-1 illustrates the production of the DLL.

public class SquareWidget

{

public double SideLength = 0; public double Area

{

get { return SideLength * SideLength; }

}

}

Figure 10-1. The SuperLib source code and the resulting assembly

To create a class library using Visual Studio 2010, select the Class Library template from the installed Windows templates. Specifically, when in Visual Studio, do the following:

1.Select File New Project, and the New Project window will open.

2.In the left pane in the Installed Templates panel, find the Visual C# node under the Other Languages root, and select the Windows entry.

3.In the right pane, select the Class Library template.

270

CHAPTER 10 NAMESPACES AND ASSEMBLIES

Suppose also that you are writing a program called MyWidgets, and you want to use the SquareWidget class. The code for the program is in a file called MyWidgets.cs and is shown in the following example. The code simply creates an object of type SquareWidget and uses the object’s members.

using System;

 

class WidgetsProgram

 

{

 

 

 

 

 

static void Main( )

 

{

 

 

 

 

 

 

SquareWidget sq = new SquareWidget();

// From class library

 

 

 

 

 

 

Not declared in this assembly

 

 

sq.SideLength = 5.0;

// Set the side length.

 

Console.WriteLine(sq.Area);

// Print out the area.

}

 

 

 

}

Not declared in this assembly

 

Notice that the code doesn’t declare class SquareWidget. Instead, you use the class defined in SuperLib. When you compile the MyWidgets program, however, the compiler must be aware that your code uses assembly SuperLib so it can get the information about class SquareWidget. To do this, you need to give the compiler a reference to the assembly, by giving its name and location.

In Visual Studio, you can add references to a project in the following way:

Select the Solution Explorer, and find the References folder underneath the project name. The References folder contains a list of the assemblies used by the project.

Right-click the References folder, and select Add Reference. There are five tabs from which to choose, allowing you to find the class library in different ways.

For our program, select the Browse tab, browse to the DLL file containing the SquareWidget class definition, and select it.

Click the OK button, and the reference will be added to the project.

271

CHAPTER 10 NAMESPACES AND ASSEMBLIES

After you’ve added the reference, you can compile MyWidgets. Figure 10-2 illustrates the full compilation process.

Figure 10-2. Referencing another assembly

272

CHAPTER 10 NAMESPACES AND ASSEMBLIES

The mscorlib Library

There’s a class library that I’ve been using in every example in the book so far. It is the one that contains the Console class. The Console class is defined in an assembly called mscorlib in a file called mscorlib.dll. You won’t find this assembly listed in the References folder, however. Assembly mscorlib contains the definitions of the C# types and the basic types for most .NET languages. It must always be referenced when compiling a C# program, so Visual Studio doesn’t bother showing it in the References folder.

When you take into account mscorlib, the compilation process for MyWidgets looks more like the representation shown in Figure 10-3. After this, I’ll assume the use of the mscorlib assembly without representing it again.

Figure 10-3. Referencing class libraries

Now suppose that your program has been working fine with the SquareWidget class, but you want to expand its capabilities to use a class called CircleWidget, which is defined in a different assembly called UltraLib. The MyWidgets source code now looks like the following. It creates a SquareWidget object as defined in SuperLib and a CircleWidget object as defined in UltraLib.

class WidgetsProgram

{

static void Main( )

{

SquareWidget sq = new SquareWidget();

// From SuperLib

...

 

CircleWidget circle = new CircleWidget();

// From UltraLib

...

 

}

 

}

 

273

CHAPTER 10 NAMESPACES AND ASSEMBLIES

The source code for class library UltraLib is shown in the following example. Notice that besides class CircleWidget, like library SuperLib, it also declares a class called SquareWidget. You can compile UltraLib to a DLL and add it to the list of references in project MyWidgets.

public class SquareWidget

{

...

}

public class CircleWidget

{

public double Radius = 0; public double Area

{

get { ... }

}

}

Since both libraries contain a class called SquareWidget, when you attempt to compile program MyWidgets, the compiler produces an error message because it doesn’t know which version of class SquareWidget to use. Figure 10-4 illustrates this name clash.

Figure 10-4. Since assemblies SuperLib and UltraLib both contain declarations for a class called

SquareWidget, the compiler doesn’t know which one to instantiate.

274

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]