Does C# have a not Conditional (!Conditional, NotConditional, Conditional(!)) attribute?
i know C# has a Conditional attribute:
[Conditional("ShowDebugString")]
public static void ShowDebugString(string s)
{
...
}
which is equivalent1 to:
public static void ShowDebugString(string s)
{
#if ShowDebugString
...
#endif
}
But in this case i want the inverse behavior (you have to specifically opt out):
public static void ShowDebugString(string s)
{
#if !RemoveSDS
...
#endif
}
Which leads me to try:
[!Conditional("RemoveSDS")]
public static void ShowDebugString(string s)
{
...
}
which doesn’t compile. And:
[Conditional("!RemoveSDS")]
public static void ShowDebugString(string s)
{
...
}
which doesn’t compile. And:
[NotConditional("RemoveSDS")]
public static void ShowDebugString(string s)
{
...
}
which doesn’t compile because it’s only wishful thinking.
1 Not true, but true enough. Don’t make me bring back the Nitpicker’s Corner.
First, having the
Conditionalattribute is not equivalent to having#ifinside the method. Consider:With the real behaviour of
ConditionalAttribute,MethodThatTakesAgesdoesn’t get called – the entire call including argument evaluation is removed from the compiler.Of course the other point is that it depends on the compile-time preprocessor symbols at the compile time of the caller, not of the method 🙂
But no, I don’t believe there’s anything which does what you want here. I’ve just checked the C# spec section which deals with conditional methods and conditional attribute classes, and there’s nothing in there suggesting there’s any such mechanism.