Alternatives to Conditional Compilation in C#

前端 未结 7 2443
感情败类
感情败类 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:36

    One thing is to use the ConditionalAttribute:

    [Conditional("DEBUG")]
    public void Foo()
    {
        // Stuff
    }
    
    // This call will only be compiled into the code if the DEBUG symbol is defined
    Foo();
    

    It's still conditional compilation, but based on attributes rather than #ifdef, which makes it generally simpler.

    Another alternative is simply to use Boolean values at execution time, instead of doing it all at compile time. If you could give us more details of what you're trying to achieve and how you're using conditional compilation, that would be useful.

提交回复
热议问题