c# - 是否有针对 CLR 版本的 C# 预编译器定义

标签 c# .net clr .net-4.0 clr4.0

我需要根据 CLR 版本有条件地编译代码。
例如,有一段代码我只需要在 CLR 2 (.NET 3.5 VS2008) 中编译,而不是在 CLR 4 (.NET 4 VS2010) 中编译
当前 CLR 版本是否有可以在 #if 子句中使用的预编译器指令?

谢谢。

最佳答案

据我所知,情况并非如此。

尝试使用您自己的,例如:

#if CLR_V2
  #if CLR_V4
    #error You can't define CLR_V2 and CLR_V4 at the same time
  #endif

  code for clr 2

#elif CLR_V4

  code for clr 4

#else
  #error Define either CLR_V2 or CLR_V4 to compile
#endif

然后您可以在 Visual Studio 的项目属性窗口或 csc 命令行参数中定义 CLR_V2 和/或 CLR_V4。

关于c# - 是否有针对 CLR 版本的 C# 预编译器定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1879743/

相关文章:

.net - 作为服务登录与作为批处理作业登录

c# - 网络共享上的 .NET 4.0 应用程序导致 SecurityException

C# "deteriorating"列表?

c# - 多个 ComboBoxes 绑定(bind)到一个公共(public)源,强制执行不同的选择

c# - 以不区分大小写的方式查找子字符串 - C#

c# - 将 TextBox 中的值传递给 ValidationRule

c# - 如何在 C# 中使用 websocket-sharp 禁用强制连续帧

c# - 使用 .NET 4.0、3.5 时,UnmanagedFunctionPointer 会导致堆栈溢出

c# - 为什么向 void 返回方法添加返回类型会导致 MissingMethodException

c# - 如何使用 GTK 使 Mono/C# 应用程序保留屏幕空间?