Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

C# LANGUAGE SPECIFICATION

1console “Class2.M executed” only. Judicious use of pp-declarations can eliminate such problems. end

2example]

324.4.2.2 Conditional Attribute Classes

4An attribute class (§24.1) decorated with the Conditional attribute is said to be a conditional attribute

5class. The Conditional attribute indicates a condition by testing a conditional compilation symbol.

6Attribute specifications (§24.2) of a conditional attribute class are either included or omitted depending on

7whether this symbol is defined at the point of specification. If the symbol is defined, the attribute

8specification is included; otherwise the attribute specification is omitted.

9[Example: The example

10#define DEBUG

11using System;

12using System.Diagnostics;

13[Conditional(“DEBUG”)]

14public class TestAttribute : Attribute {}

15[Test]

16class Class1 {}

17declares attribute class TestAttribute as a conditional attribute class. The attribute section applies the

18Test attribute to Class1. Since the conditional compilation symbol DEBUG is defined, retrieving the

19attributes applied to Class1 at runtime will result in an instance of the TestAttribute class. If the

20symbol DEBUG had not been defined, then retrieving the attributes applied to Class1 at runtime would not

21result in an instance of the TestAttribute class. end example]

22It is important to note that the inclusion or exclusion of an attribute specification of a conditional attribute

23class is controlled by the conditional compilation symbols at the point of the specification. [Example: In the

24example

25File test.cs:

26using System;

27using System.Diagnostics;

28[Conditional(“DEBUG”)]

29public class TestAttribute : Attribute {}

30File class1.cs:

31#define DEBUG

32

[Test]

// TestAttribute is specified

33class Class1 {}

34File class2.cs:

35#undef DEBUG

36

[Test]

// TestAttribute is not specified

37class Class2 {}

38the classes Class1 and Class2 are each decorated with attribute Test, which is conditional based on

39whether or not DEBUG is defined. Since this symbol is defined in the context of Class1 but not Class2, the

40specification of the Test attribute on Class1 is included, while the specification of the Test attribute on

41Class2 is omitted. end example]

4224.4.3 The Obsolete attribute

43The attribute Obsolete is used to mark types and members of types that should no longer be used.

376

Chapter 24 Attributes

1If a program uses a type or member that is decorated with the Obsolete attribute, then the compiler shall

2issue a warning or error in order to alert the developer, so the offending code can be fixed. Specifically, the

3compiler shall issue a warning if no error parameter is provided, or if the error parameter is provided and has

4the value false. The compiler shall issue a compile-time error if the error parameter is specified and has the

5value true.

6[Example: In the following code

7[Obsolete("This class is obsolete; use class B instead")]

8class A

9{

10public void F() {}

11}

12class B

13{

14public void F() {}

15}

16class Test

17{

18

static void Main() {

19

A a = new A(); // warning

20

a.F();

21}

22}

23the class A is decorated with the Obsolete attribute. Each use of A in Main results in a warning that

24includes the specified message, “This class is obsolete; use class B instead.”

25end example]

377

Соседние файлы в предмете Электротехника