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

I have a bunch of unit tests that need to be conditionally compiled based on the Windows OS version. These unit tests test TxF, which is only available on Windows Vista and higher.

#if WIN_OS_VERSION >= 6.0 // Run unit tests #endif 
+6
c # conditional-compilation
source share
2 answers

I don’t think there is a way to conditionally compile code based on the OS version. Documentation for #define states (highlighting mine):

Symbols can be used to indicate conditions for compilation. You can check the C # if or #elif character. You can also use the conditional attribute to perform conditional compilation.

You can define a character, but you cannot assign a value to a character. The #define directive should appear in the file before using any instructions that are not directives as well.

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

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

The volume of a character created with #define is the file in which it was defined.

You will have to conditionally run it:

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

Update:

This is set to .

+4
source share

You can just manage your debugging symbols yourself.

just come up with a scheme you can stick to ( Document It! ), and then when you compile the new platform, just remember to change the processor directives.

for example you could have characters

 LATER_THAN_XP LATER_THAN_VISTA etc... 

Then you can use #ifdef for conditional compilation

 #ifdef LATER_THAN_XP //Run Unit Tests #endif 

Then you can simply define these constants in your project properties. Or, if you feel like an adventurer, you can probably define an MSBuild task that exports the correct character (s) to be determined at compile time, but that means litle is above my pay level.

0
source share

All Articles