Or operator in Conditional attribute in C#
C#C# Problem Overview
In C# we can differentiate code execution depending on the type of build. By default we have Debug and Release types defined.
We can do it using the #if
directive:
#if DEBUG
public void Foo()
{ ... }
#endif
But we can also use Conditional
attribute:
[Conditional("DEBUG")]
public void Foo()
{ ... }
The second solution is even claimed to be more maintainable (see: Effective C# by Bill Wagner).
My question is - how can I use the Conditional
attribute with many build configurations? Is it possible to somehow use the or
operator? I ask because I want some Foo method to be executed both in, for example, the DEBUG and BAR build configurations. What then?
C# Solutions
Solution 1 - C#
You can use multiple comma separated conditional attributes like
[Conditional("DEBUG"), Conditional("BAR")]
and it will be exactly your desired behaviour - they will be logically ORed together.
See MSDN for reference.