开发者

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

开发者 https://www.devze.com 2022-12-30 01:13 出处:网络
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.

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


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.


You can simply manage the debug symbols yourself.

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

for example you could have symbols

LATER_THAN_XP
LATER_THAN_VISTA
etc...

Then you can use #ifdef's to conditionally compile

#ifdef LATER_THAN_XP

//Run Unit Tests

#endif

Then you can just define these constants in your project properties. Or if you are feeling adventurous you could probably define an MSBuild Task that exports the correct symbol(s) to define at compilation time, but thats a litle above my pay grade.

0

精彩评论

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

关注公众号