What is a .NET attribute to prevent a method from compiling in release mode?

I know that if I have a block of code that I don't want to compile, when in release mode I can wrap that block of code in:

#if DEBUG
   while(true)
{ Console.WriteLine("StackOverflow rules"); }
#endif

      

This will prevent this code block from being compiled in any mode other than DEBUG

.

I know there is an attribute that can be put on an entire method that will do the same, but for the life of me I can't remember what that attribute is. I believe it's in the namespace System.Diagnostics

, but I'm not sure.

BTW: I am using .NET 4, but I know this attribute exists in .NET 2 because I used it in older projects.

thanks

+2


a source to share


3 answers


This is ConditionalAttribute .

Tells compilers that a method call or attribute should be ignored unless the specified compilation conditional symbol.



You must define it as [Conditional("DEBUG")]

and make sure the DEBUG constant is not defined in release mode.

+3


a source


As an alternative to the conditional attribute, you can simply use:



#if (!DEBUG) 

      

0


a source


When using ConditionalAttribute, remember that it cannot be used for functions that return anything but void, or take an out-parameter as an argument. The ref parameter is great as this variable is created before the method is called.

[Conditional("DEBUG")]
public void Success1(string param)

[Conditional("DEBUG")]
public void Success2(ref string param)

[Conditional("DEBUG")] // out parameter
public void CompileErrorCS0685(out string param)

[Conditional("DEBUG")] // non-void function
public bool CompileErrorCS0578(string param)

      

0


a source







All Articles