替代条件编译在C#

问题描述:

什么是替代其在C#中使用条件编译的代码?

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

我有了很多的代码的类,它基于的#ifdef ..一段时间后我的代码不可读。

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

寻找重构技术用于更好的可读性和维修的许多代码#如果 DEFS

Looking for refactoring techniques to use for better readability and maintenance of code with many #if defs

有一件事是使用的 ConditionalAttribute

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();



这还是条件编译,但基于属性而非 #IFDEF ,这使得它一般简单。

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.