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

CHAPTER 7 CLASSES AND INHERITANCE

Inheritance Between Assemblies

So far, I’ve been declaring derived classes in the same assembly that contains the base class. But C# also allows you to derive a class from a base class defined in a different assembly. To do this, the following must be true:

The base class must be declared public so that it can be accessed from outside its assembly.

You must include a reference in your Visual Studio project to the assembly containing the base class.

To make it easier to refer to the classes and types in the other assembly, without using their fully qualified names, place a using directive at the top of the source file, with the namespace containing the classes or types you want to access.

Note Adding a reference to the other assembly and adding a using directive are two separate things. Adding the reference to the other assembly tells the compiler where the required types are defined. Adding the using directive allows you to reference other classes without having to use their fully qualified names. Chapter 10 covers this in detail.

For example, the following two code segments, from different assemblies, show how easy it is to inherit a class from another assembly. The first code listing creates an assembly that contains the declaration of a class called MyBaseClass, which has the following characteristics:

It’s declared in a source file called Assembly1.cs and inside a namespace declared as BaseClassNS.

It’s declared public so that it can be accessed from other assemblies.

It contains a single member, a method called PrintMe, that just writes out a simple message identifying the class.

//Source file name Assembly1.cs

using System;

Namespace containing declaration of base class

namespace BaseClassNS

{ Declare the class public so it can be seen outside the assembly.

public class MyBaseClass { public void PrintMe() {

Console.WriteLine("I am MyBaseClass");

}

}

}

182

CHAPTER 7 CLASSES AND INHERITANCE

The second assembly contains the declaration of a class called DerivedClass, which inherits from MyBaseClass, declared in the first assembly. The source file is named Assembly2.cs. Figure 7-14 illustrates the two assemblies.

DerivedClass has an empty body but inherits method PrintMe from MyBaseClass.

Main creates an object of type DerivedClass and calls its inherited method PrintMe.

//Source file name Assembly2.cs

using System; using BaseClassNS;

Namespace containing declaration of base class namespace UsesBaseClass

{

Base class in other assembly

 

 

 

class

DerivedClass: MyBaseClass {

//

Empty body

}

 

 

 

class Program {

static void Main( )

{

DerivedClass mdc = new DerivedClass(); mdc.PrintMe();

}

}

}

This code produces the following output:

I am MyBaseClass

Figure 7-14. Inheriting across assemblies

183

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