Книга: C# 2008 Programmer

#line

#line

The #line preprocessor directive lets you modify the compiler's line number and (optionally) the file name output for errors and warnings.

The #line preprocessor directive is injected in the following example. The highlighted code indicates statements that will cause the debugger to issue warning messages:

1.  using System;
2.  using System.Collections.Generic;
3.  using System.Linq;
4.  using System.Text;
5.
6.  namespace TestDefine
7.  {
8.   class Program
9.   {
10.   static void Main(string[] args)
11.   {
12. #line 25
13.    int i; //---treated as line 25---
14.    char c; //---treated as line 26---
15.    Console.WriteLine("Line 1"); //---treated as line 27---
16. #line hidden //---treated as line 28---
17.    Console.WriteLine("Line 2"); //---treated as line 29---
18.    Console.WriteLine("Line 3"); //---treated as line 30---
19. #line default
20.    double d; //---treated as line 20---
21.    Console.WriteLine("Line 4"); //---treated as line 21---
22. #line 45 "Program1.cs" //---treated as line 22---
23.    Single s; //---treated as line 45---
24.    Console.WriteLine("Line 5"); //---treated as line 46---
25.    Console.ReadLine(); //---treated as line 47---
26.   }
27.  }
28. }

The line numbers are for illustration purposes and are not part of the program.

The four highlighted lines are numbered 13, 14, 20, and 23. When you build the program in Visual Studio 2008, the lines reported are 25, 26, 20, and 45 (see Figure 3-18).


Figure 3-18 

Let's take a look at the #line directives in the example program:

#line 25 means that you want to modify the line number to use the specified line number (25 in this case) instead of the actual line number of the statement in error. This is useful if you need to assign a fixed line number to a particular part of the code so that you can trace it easily. Interestingly, the next line will continue from 25, that is, the next line is now line 26. This is evident from the warning message for the char c; line.

#line default means that the compiler will report the actual line number.

#line 45 "Program1.cs" means that you want to fix the line number at 45 and specify the name of the file in error (Program1.cs in this case). An example usage of it would be that the statement in error might be a call to an external DLL and by specifying the filename of the DLL here, it is clearer that the mistake might be from that DLL.

What about the #line hidden statement? That preprocessor directive indicates to the debugger to skip the block of code beginning with the #line hidden preprocessor directive. The debugger will skip the line(s) until the next #line preprocessor directive is found. This is useful for skipping over method calls that you are not interested in (such as those not written by you).

Interestingly, you can replace the #line hidden preprocessor directive with #line 16707566 (0xFeeFee) and it will still work correctly.

Оглавление книги


Генерация: 1.510. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз