Microsoft KB Archive/922783

= A method that is decorated with the ConditionalAttribute attribute may still be called at run-time in an application that is built on the Microsoft .NET Framework 2.0 =

Article ID: 922783

Article Last Modified on 11/22/2007

-

APPLIES TO


 * Microsoft ASP.NET 2.0
 * Microsoft Visual Studio 2005 Express Edition
 * Microsoft Visual Studio 2005 Professional Edition
 * Microsoft Visual Studio 2005 Standard Edition
 * Microsoft Visual Studio 2005 Team System Architect Edition
 * Microsoft Visual Studio 2005 Team System Developer Edition
 * Microsoft Visual Studio 2005 Team Foundation
 * Microsoft Visual Studio 2005 Team System Test Edition

-





SYMPTOMS
When you apply the ConditionalAttribute attribute to a method to prevent calls to the method at run-time, the method might still be called at run-time. This problem occurs when the application is built on the Microsoft .NET Framework 2.0.



CAUSE
This problem occurs because methods that are decorated with the ConditionalAttribute attribute are always compiled into Microsoft intermediate language (MSIL). Although the compiler does not allow for calls to conditional methods if the condition is not met, reflection-based calls to those methods can still be made at run-time.



WORKAROUND
To work around this behavior, enclose the code that you want to compile conditionally by using the #if compiler directive. For example, to cause a method to be available only when the application is compiled in debug mode, put the code on a new line before the method block:


 * 1) if DEBUG

Then, put the following code on a new line after the end of the method:


 * 1) endif



MORE INFORMATION
For more information about how to use the ConditionalAttribute attribute, visit the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/library/system.diagnostics.conditionalattribute(vs.71).aspx

Keywords: kbtshoot kbinfo kbprb KB922783

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.