Conditional compilation without polluting your code?

You know how you can put Debug and Trace statements in your code and as long as you uncheck the Debug or Trace option in your project properties, those statements are removed from your compiled version. Well, is there a way that I can achieve that functionality with some statements of my own (not Debug or Trace) without polluting my code with a bunch of crappy conditional compilation statements surrounding every statement I want left/removed?

Perhaps an attribute or something?

[494 byte] By [ItchyAshcrackistan] at [2007-12-23]
# 1

Pretty easy:



[Conditional("DEBUG")]
public void DoSomethingDebug()
{
//Stuff goes here
}
...
public void Caller()
{
//The next call will not be compiled in if DEBUG is not defined.
DoSomethingDebug();
}

By adding the Conditional Attribute (in System.Diagnostics) you get the same functionality used by Trace and Debug.

PhilipRieck at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified