开发者

#include in C# (conditional compilation)

Is it possible in C# to set such a condition that if the condition is true - compile one file; if the condition is false - compile another file?

开发者_如何转开发

Sort of like

#ifdef DEBUG
#include Class1.cs
#else
#include Class2.cs
#endif

Or possibly set it up in project properties.


No, it isn't.

However, you can wrap both entire files in #if blocks.

You might also want to look at the [Conditional] attribute.


I wouldn't recommend it. I don't like the idea of Debug and Release having such wildly different code that you need to have two totally separate files to make sense of the differences. #if DEBUG at all is a pretty big code smell IMO...

However, you could do it like this:

// Class1.cs
#if DEBUG

...

#endif

.

// Class2.cs
#if !DEBUG

...

#endif


In C# we don't use an include of a file, but you can use conditional methods.

For instance, if I'm developing a game and I'm using a shared code base for my input class, but I want to have one method called if I'm on an Xbox, and a different method get called if I'm on a Zune. It's still going to return the same class of input data, but it's going to take a very different route to get it.

You learn more about conditional methods here: http://msdn.microsoft.com/en-us/library/aa288458(v=VS.71).aspx


Thankfully no, not in the preprocessor like that. You can, however, exclude the implementation of a whole file within the file itself. Or you can set up a build process with MSBuild or NAnt to switch around our files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜