Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

C#

INSERT ATTRIBUTES

C# provides attributes so you can specify additional information about the functionality of a string of code. For example, you can add an attribute to a

string of code that points to another file for the program to open or to a type in another program.

Attributes retrieve information by using the reflection process. When your C# program runs, the attributes obtain information about program assemblies — the collection of types used in your program — and the types within those assemblies including classes, interfaces, and value types.

When your C# program encounters an attribute in a string of code, the program invokes the attribute. For example, if

your C# program encounters an attribute for accessing an external file, such as a text file, the file will open when you and your users access that string of code.

C# contains three reserved attributes: AttributeUsage, which decribes how a custom attribute class is used; Conditional, which marks a conditional method; and Obsolete, which marks a piece of unusable code. You can also create your own custom attributes by definining an attribute class. C# gives you the building blocks of attribute creation by deriving all custom attributes from the built-in

System.Attribute class.

INSERT ATTRIBUTES

Console

Applicatio

¤ Click New Project in the

Start page.

The New Project window appears.

Click the Console Application icon in the Templates pane.

Type a name for your file.

ˇ Click OK.

WORKING WITH VISUAL C# BASICS 3

The Visual C# reflection system comes in the form of a built-in method called GetCustomAttributes. The GetCustomAttributes class returns those custom attributes as an array of objects that you can view in your program output.

TYPE THIS:

using System; [Obsolete]

class GetAttribute

{

static void Main(string[] args)

{

Type x = typeof(GetAttribute);

object[] xget = x.GetCustomAttributes(); Console.WriteLine(“The custom attribute is:”); foreach (object y in xget)

Console.WriteLine (y);

}

}

RESULT:

The custom attribute is

System.ObsoleteAtt ribute.

The Class1.cs code appears in the parent window.

Á Delete the summary description code for Class1.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Type the preprocessor

° Remove the comments

· Type code that outputs a

and additional System

within the Main method.

string from the Main method

namespace at top, the class

 

 

and then gives permission to

to output data, and the output

 

 

access the One method.

statement for the first method.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CONTINUED

 

47

C#

INSERT ATTRIBUTES

C# provides three built-in attributes. One provides the necessary building block for creating custom attributes. The other two attributes provide common

attribute functions in C# programs: making a method conditional and marking a string of code that is obsolete. For example, you can set an attribute that will run a method only if the user-entered password matches the password in your program.

The AttributeUsage attribute lets you define where you can apply your attributes. Visual C# contains built-in attribute values called AttributeTargets that let you determine what elements should include the attribute, such as a class.

The Conditional attribute lets you determine whether a string in your program will let a method execute. The Conditional attribute looks for a preprocessing identifier, such as input from the user, that tells the program whether it should execute a particular method or skip the entire method.

The Obsolete attribute lets you mark a section of code as that not to be used. You can set the Obsolete attribute to display a message when you encounter the code in your program (which is the preferred method so you can inform users about other options), or as a Boolean false attribute that will generate a compiler warning when you access that part of the program.

INSERT ATTRIBUTES (CONTINUED)

Main method

± Run the program by

calls NewMethod.

pressing the F5 key.

The Main method string appears first followed by the One method string and the

NewMethod string.

WORKING WITH VISUAL C# BASICS 3

The AttributeTargets class not only lets you specify the attribute target for an assembly or module but also for classes.

TYPE THIS:

using System; [AttributeUsage(AttributeTargets.Class)] class information Info : SystemAttribute

{

public Info(string name);

}

The AttributeTargets.Class argument within the AttributeUsage attribute tells your program that the program attributes apply to a class and not to any other target.

¡ Change the NewMethod

code in the Main method to

OldMethod.

Run the program by

£ Save the program as the

pressing the F5 key.

filename.

The obsolete method

 

prompts the Task List to report

 

that you are going the wrong

 

way.

 

49

Соседние файлы в папке c#