
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
446
|
Chapter 8: Diagnostics
public void TraceHelp(string message)
{
...
}
The TraceHelp method is compiled only when the TRACE preprocessing identifier is
defined.
Discussion
The ConditionalAttribute attribute can be used only on methods. It prevents them
from being compiled and called at runtime when the preprocessor identifier passed
to the
ConditionalAttribute constructor is undefined. Properties, indexers, and other
members cannot have this attribute.
Another limitation of this attribute is that it can be placed only on a method that
returns
void. This makes sense, since code that invokes this method doesn’t expect a
return value and will run successfully whether or not the method is invoked. For
example, in the code:
int retValue = Car.GetModelNumber( );
if the GetModelNumber method is not compiled, then this code will not be able to func-
tion correctly.
Along these same lines, a method marked as
override cannot be marked with the
ConditionalAttribute attribute. However, the virtual method that a method over-
rides may be marked with the
ConditionalAttribute attribute. If the virtual method
is marked with this attribute, all methods overriding it are compiled and called based
on whether the virtual method is compiled. In other words, the overriding methods
are implicitly marked with the