Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

84

Part II

HANDLING DATA

 

 

 

In Chapter 3, “Components of C#,” you learned about classes and the methods implemented with classes. In this chapter, you will learn about attributes and

properties that are used to store extra information about classes.

Attributes

Attributes are used to store additional information about methods and classes. You have extensively used attributes in the previous chapters. For example, the class and method modifiers that store accessibility information about classes and methods, respectively, are attributes placed on these entities. Attributes are elements used with methods, classes, assemblies, and Web services. Attributes can also be used with arguments of a method.

Attributes are similar to preprocessor directives, as the attributes are not compiled during the execution of a program. However, attributes are useful because they provide you with additional information about resources in a program. You can retrieve this information at run time and can then document the information for future use. To retrieve the information stored in attributes, you need to create instances of the Attribute class. You will learn about the Attribute class later in this chapter.

Declaring Attributes

Attributes are declared using an attribute declaration statement, such as:

[attribute name (attribute parameters)]

This statement includes the name of the attribute, followed by the list of parameters in parentheses. You can also define an attribute that does not take any parameter. The attribute declaration statement is immediately followed by the declaration of the entities for which the attribute is defined.

All attributes in C# are derived from the Attribute class. The Attribute class is global to the .NET Framework.This implies that if you declare an attribute, it can

ATTRIBUTES AND PROPERTIES

Chapter 5

85

 

 

 

be used by any class defined in the .NET Framework. The next section will discuss the Attribute class in detail.

Attribute Class

You can define an attribute class to store user-defined attributes. The attribute class that you declare also contains information about the entities on which you can place the attributes defined in the class. All attribute classes are derived from the abstract class Attribute. The Attribute class is contained in the System namespace.

Once an attribute is declared in the attribute class, you can place the attribute on any entity. An attribute class can be of the following types:

Single-use attribute class. The attributes declared in a single-use attribute class cannot be placed more than once on the same entity.

[color (“Green”)] class Car

{

--------------

}

Multiuse attribute class. The attributes declared in a multiuse attribute class can be placed more than once on the same entity. The following example shows that two values of the attribute color can be placed on the class Car.

[color (“Green”), color (“Blue”)] class Car

{

--------------

}

As discussed earlier, you can create attributes that take parameters. To perform this task, you need to define a parameter list in the default constructor of the attribute class. The attribute class takes two types of parameters. These parameters will now be described in detail.

86

Part II

HANDLING DATA

 

 

 

Attribute Parameters

The parameters used with attributes include the following:

Positional parameters. Parameters that are declared in the public constructor of the attribute class are called positional parameters. A positional parameter of an attribute consists of an attribute argument expression and is used with the required parameters.

Named parameters. Parameters that are declared in the non-static, public read-write field or property of an attribute class are called named parameters. They are used to read and write values to an attribute. You use named parameters to define optional parameters of an attribute class.

TIP

If you need to declare both positional and named parameters for the same attribute class, the positional parameters are followed by the named parameters.

You can also specify the data types of both positional and named parameters. The attribute parameter types supported by C# are int, short, long, byte, char, string, bool, double, float, object, type, and enum. You have learned about these data types in Chapter 2, “C# Basics,” in the section “Variable Data Types.”

Until now, you have seen that you can define custom attributes and the custom attribute class. However, C# also contains certain default attributes, as discussed in the next section.

Default Attributes

The C# compiler explicitly recognizes the default parameters provided by C# and compiles the program code accordingly. Following are some of the most commonly used default attributes.

Obsolete attribute. As the name suggests, the Obsolete attribute is used to mark an element that you should no longer use in any program code. The Obsolete attribute is the alias defined for the ObsoleteAttribute class in the System namespace. To prevent a programmer from using the code marked obsolete, you can generate an error or a warning by passing

ATTRIBUTES AND PROPERTIES

Chapter 5

87

 

 

 

the error or warning as a parameter to the Obsolete attribute. For example:

[Obsolete (“Do not use this method in the code”), true]

Here, the first parameter contains the error or warning message to be displayed. The second parameter of the type bool specifies whether an error or a warning will be generated. The value of true specifies that the compiler will generate an error and stop the execution of the program. However, if the value of this parameter is false, the compiler only generates a warning.

Conditional attribute. The Conditional attribute is used to conditionally compile a set of statements marked with the Conditional attribute. You can also mark a method with the Conditional attribute. The method or set of statements will then be compiled only if a symbol is defined. Consider the following example:

[Conditional (“Symbol1”)] public void Method1()

{

------------

}

In this code, the call to the function Method1 will only be made if Symbol1 is defined. Symbol1 can be defined using the #define preprocessor directive. You have learned about the preprocessor directives available in C# in Chapter 4, “More about Components,” in the section “Preprocessor Directives.”

AttributeUsage attribute. The AttributeUsage attribute is used with the attribute class. This attribute takes parameters that store information about the attribute class. To know more about the AttributeUsage attribute, consider the following example:

[AttributeUsage (AttributeTargets.Class | AttributeTargets.Structs,

AllowMultiple = true, Inherited = true)]

public class Attribute1 : Attribute

{

-----------

}