Alternatives to Conditional Compilation in C#

前端 未结 7 2455
感情败类
感情败类 2021-02-07 21:17

What is the alternative to having code with conditional compilation in C#?

I have a class that has lots of code that is based on # ifdef .. After sometime my code is unr

7条回答
  •  忘了有多久
    2021-02-07 21:41

    Expanding on Jon's answer, while there a number of limitations on using the ConditionalAttribute, there is a significant advantage. When the condition is false, the calls to conditional method are omitted. For example, you can add 100's of calls to a logging system, say for debugging, that can be conditionally excluded from the production code. When they are excluded, there is no overhead associated with calling a method which does not need to be called. Using #ifdef, you would have to wrap every call to the logging system to conditionally exclude them.

    Note this only works across assemblies, if the caller of the conditional method is re-compiled.

提交回复
热议问题