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 (922783)



The information in this article 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 System 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:

#if DEBUG

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

#endif

MORE INFORMATION

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

Modification Type:MajorLast Reviewed:8/22/2006
Keywords:kbtshoot kbinfo kbprb KB922783 kbAudDeveloper