So you need to selectively include and exclude sections of source code from your compiled assembly?
Use the #if, #elif, #else, and #endif preprocessor directives to identify blocks of code that should be
conditionally included in your compiled assembly. Use the System.Diagnostics.ConditionalAttribute
attribute to define methods that should be called conditionally only. If you apply ConditionalAttribute to a method, the compiler will ignore any calls to the method if the symbol specified by ConditionalAttribute is not defined at the calling point.Control the inclusion of the conditional code using the #define and #undef directives in your code, or use the /define switch when you run the C# compiler from the command line.
Using ConditionalAttribute centralizes your conditional compilation logic on the method
declaration and means you can freely include calls to conditional methods without littering your code
with #if directives. However, because the compiler literally removes calls to the conditional method
from your code, your code can’t have dependencies on return values from the conditional method. This
means you can apply ConditionalAttribute only to methods that return void and do not use “out”
modifiers on their arguments.
[Conditional("DEBUG")]
public static void DumpState()
{
Console.WriteLine("Dump some state...");
}
No comments:
Post a Comment