Is it possible to conditionally compile classes and methods based on some constant value defined in a class, for example I\'d like to have something like that.
p
No -
C# does not evaluate expressions in the #if preprocessor directive - it only looks to see if the symbol is defined :
When the C# compiler encounters an
#if
directive, followed eventually by an#endif
directive, it compiles the code between the directives only if the specified symbol is defined. Unlike C and C++, you cannot assign a numeric value to a symbol. The #if statement in C# is Boolean and only tests whether the symbol has been defined or not.
(emphasis added)
So the proper way to do what you want is to do:
public class SomeClass {
#if BUILD_MODE_ONE
public void SomeMethod() { /* Implementation one */ }
#elif BUILD_MODE_TWO
public void SomeMethod() { /* Implementation two */ }
#elif BUILD_MODE_THREE
public void SomeMethod() { /* Implementation three */ }
#endif
}
Where the symbol could either be defined in that code file or via command-line parameters to CSC.EXE.