gpt4 book ai didi

c# - 在 C# 中是否有一种简单的方法可以根据操作系统版本获得条件编译符号

转载 作者:太空狗 更新时间:2023-10-29 21:47:41 24 4
gpt4 key购买 nike

我有一堆单元测试需要根据 Windows 操作系统版本进行条件编译。此单元测试正在测试仅在 Windows Vista 及更高版本中可用的 TxF。

#if WIN_OS_VERSION >= 6.0
// Run unit tests
#endif

最佳答案

我不认为有一种方法可以根据操作系统版本有条件地编译代码。 #define 状态的文档(强调我的):

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.

您必须有条件地运行它:

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

更新:

有人问过before .

关于c# - 在 C# 中是否有一种简单的方法可以根据操作系统版本获得条件编译符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2830957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com