1. Overview
This chapter covers how to select the appropriate build type, create and manage compilation directives, hypervisor data Files (PDB), and directives.
2. Main content
2.1 Build Types
The two default build modes in. NET are release mode and debug (Debug) mode.
2.2 Creating and managing compilation directives
① precompiled directives: precompiled directives in C # that are used to adjust logic during compilation.
Public void debugdirective () { #if Console.WriteLine ("Debug mode"); #else Console.WriteLine ("Not Debug"); #endif}
* DEBUG instructions are system-brought, and users can also use # define to customize precompiled directives. Using custom precompiled directives reduces the readability of the program and is generally not recommended.
A common application scenario for precompiled directives is when writing a class library that targets multiple platforms.
public Assembly Loadassembly<t> () { #if ! WINRT assembly assembly = typeof (T). Assembly; #else assembly assembly = typeof (T). GetTypeInfo (). Assembly; #endif return Assembly }
The system comes with precompiled directives: #undef, #warning, #error, #line, #pragma.
#warning This code is obsolete #if#error#endif
#line"Otherfilename" intA// Line#lineDefault intb//Line 4#lineHidden intC//HiddenintD//Line 7
#pragma int #pragma while (false#pragma warning restore
To perform a specific method only in debug mode, you can use ConditionalAttribute in addition to #ifdebug.
Private Static void Log (string message) {Console.WriteLine ("Message");}
DebuggerDisplayAttribute can be used to output a string in debug mode in the specified format.
[debuggerdisplay ("Name = {firstname} {lastname ")] public class person { public get ; ; } public string lastname { get ; set ;&NBSP;}}
2.3 Managing program Data files and symbols
When compiling a program, you can choose to create a program data file (. pdb) that holds additional information that is useful for debugging.
A PDB file contains two pieces of information: ① source program file name and line number; ② local variable name.
* Microsoft Symbol server provided by Microsoft can be used to solve the problem of missing system PDB files.
* You can use Team Foundation server (TFS) to create your own Sympol server so that you can debug each version of the program without the source code.
* It is recommended that each compiled PDB file be saved. When publishing to a public location, you can use the Pdbcopy tool to remove private information from the PDB file.
Pdbcopy mysymbols.pdb publicsymbols.pdb–p
3. Summary
The build configuration feature of ①visual Studio can be used to configure the compiler.
The program generated by the ② in debug mode is not code-optimized and contains additional debugging information.
③ programs generated with release mode are code-optimized and can be published to a production environment.
The ④ compiler instruction can provide additional explanatory information to the compiler. can be used to introduce code in a specific build mode or to issue a warning.
⑤ a PDB file contains additional information that you need to use when debugging.
16th Debug and safety (in. net4.5) Debug Program