Q & A regarding configuration and conditionals
Another reader question of general interest.I received the following question in email the other day:
I have a question regarding the c# conditional attribute. Is it possible to set the condition using web.config or app.config files? I am trying to set a condition in the web.config/app.config and compile a method if my condition is not defined in the web.config/app.config.
I thought it would be of general interest, so I’ll post the answer here:
The argument to the conditional attribute is evaluated at compile time, not at runtime. That means you cannot set the condition in your app.config or web.config files.
To achieve the behavior you want, you can use the TraceSwitch class. I discuss this in Item 36 of Effective C#. The example I give are not exactly the same as the conditional attribute, but you can achieve the effect of the ConditionalAttribute by checking the value of the switch inside the method, and proceeding only if the desired value is present.