6

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
4

2 回答 2

4

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.

于 2010-05-13T23:33:10.703 回答
0

您可以简单地自己管理调试符号。

只需提出一个您可以坚持使用的模式(Document It!),然后当您为新平台编译时,请记住更改处理器指令。

例如你可以有符号

LATER_THAN_XP
LATER_THAN_VISTA
etc...

然后就可以使用#ifdef's有条件地编译

#ifdef LATER_THAN_XP

//Run Unit Tests

#endif

然后你可以在你的项目属性中定义这些常量。或者,如果您喜欢冒险,您可以定义一个 MSBuild 任务,该任务导出正确的符号以在编译时定义,但这比我的工资等级高一点。

于 2010-05-14T00:03:55.387 回答