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

CHAPTER 23 PREPROCESSOR DIRECTIVES

Diagnostic Directives

Diagnostic directives produce user-defined compile-time warning and error messages.

The following is the syntax of the diagnostic directives. The messages are strings, but notice that unlike normal C# strings, they do not have to be enclosed in quotation marks.

#warning Message

#error Message

When the compiler reaches a diagnostic directive, it writes out the associated message. The diagnostic directive messages are listed by the compiler along with any compiler-generated warning and error messages.

For example, the following code shows an #error directive and a #warning directive.

The #error directive is inside an #if construct so that it will be generated only if the conditions on the #if directive are met.

The #warning directive is a reminder to the programmer to come back and clean up a section of code.

#define RightHanded #define LeftHanded

#if RightHanded && LeftHanded

#error Can't build for both RightHanded and LeftHanded #endif

#warning Remember to come back and clean up this code!

635

CHAPTER 23 PREPROCESSOR DIRECTIVES

Line Number Directives

Line number directives can do several things, including the following:

Change the apparent line numbers reported by the compiler’s warning and error messages

Change the apparent file name of the source file being compiled

Hide a sequence of lines from the interactive debugger

The syntax for the #line directives is the following:

#line integer

// Sets

line

number of next

line

to value

of integer

#line "filename"

// Sets

the

apparent

filename

 

 

#line default

// Restores

real line number and

filename

 

#line hidden

//

Hides

the

following code

from

stepping

debugger

#line

//

Stops

hiding from

debugger

 

 

The #line directive with an integer parameter causes the compiler to consider that value to be the line number of the following line of code. Numbering of the subsequent lines continues, based on that line number.

To change the apparent file name, use the file name, inside double quotes, as the parameter. The double quotes are required.

To return to true line numbering and the true file name, use default as the parameter.

To hide a segment of code from the step-through-code feature of the interactive debugger, use hidden as the parameter. To stop hiding, use the directive with no parameter. This feature has, so far, mostly been used in ASP.NET and WPF for hiding compiler-generated code.

The following code shows examples of the line number directives:

#line 226

x = y + z; // Now considered by the compiler to be line 226

...

#line 330 "SourceFile.cs" // Changes the reported line number and filename var1 = var2 + var3;

...

#line default

// Restores true line numbers and filename

636

CHAPTER 23 PREPROCESSOR DIRECTIVES

Region Directives

The region directive allows you to mark, and optionally name, a section of code. The characteristics of the #region directive are the following:

It is placed on the line above the section of code you want to mark.

It can take an optional string of text following it on the line, which serves as its name.

It must be terminated by an #endregion directive, further down in the code.

Although region directives are ignored by the compiler, they can be used by source code tools. Visual Studio, for example, allows you to easily hide or display regions.

As an example, the following code has a region called Constructors, which encloses the two constructors of class MyClass. In Visual Studio, you could collapse this region to a single line when you didn’t want to see it in the code and then expand it again when you needed to work on it or add another constructor.

#region Constructors MyClass()

{

...

}

MyClass(string s)

{

...

}

#endregion

Regions can be nested, as shown in Figure 23-3.

Figure 23-3. Nested regions

637

CHAPTER 23 PREPROCESSOR DIRECTIVES

The #pragma warning Directive

The #pragma warning directive allows you to turn off warning messages and to turn them back on.

To turn off warning messages, use the disable form with a comma-separated list of warning numbers you want to turn off.

To turn warning messages back on, use the restore form with a list of the warning numbers you want to turn back on.

For example, the following code turns off two warning messages: 618 and 414. Further down in the code, it turns on messages for 618 but leaves the messages for 414 turned off.

Warning messages to turn off

#pragma warning disable 618, 414

... Messages for the listed warnings are off in this section of code.

#pragma warning restore 618

If you use either form without a warning number list, the command then applies to all warnings. For example, the following code turns off, and then restores, all warning messages.

#pragma warning disable

... All warning messages are turned off in this section of code.

#pragma warning restore

... All warning messages are turned back on in this section of code.

638

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