Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way in C# to have conditional compilation symbols based on OS version

I have a bunch of unit tests that need to be conditional compiled based on Windows OS version. This unit tests are testing TxF that is only available in Windows Vista and above.

#if WIN_OS_VERSION >= 6.0
// Run unit tests
#endif
like image 801
John Simons Avatar asked May 13 '10 23:05

John Simons


1 Answers

I don't think there's a way to conditionally compile code based on OS version. The documentation for #define states (emphasis mine):

Symbols can be used to specify conditions for compilation. You can test for the symbol with either #if or #elif. You can also use the conditional attribute to perform conditional compilation.

You can define a symbol, but you cannot assign a value to a symbol. The #define directive must appear in the file before you use any instructions that are not also directives.

You can also define a symbol with the /define compiler option. You can undefine a symbol with #undef.

A symbol that you define with /define or with #define does not conflict with a variable of the same name. That is, a variable name should not be passed to a preprocessor directive and a symbol can only be evaluated by a preprocessor directive.

The scope of a symbol created by using #define is the file in which it was defined.

You will have to conditionally run it instead:

void TestTxF() {
    if (System.Environment.OSVersion.Version.Major < 6) {
        // "pass" your test
    }
    else {
        // run it
    }
}

Update:

This has been asked before.

like image 163
Jon Avatar answered Oct 03 '22 04:10

Jon