C# 编译器是否设置版本或平台常量?

发布于 2024-07-14 19:45:32 字数 256 浏览 5 评论 0原文

C# 编译器中是否有任何预定义常量来检测目标平台的版本?

我可以在项目选项中设置平台,但如何为 .NET 2 和 .NET 3.5 编写单一源代码?

编辑:我真的想编写一些代码一次,并且能够使用 #if CLR_VERSION35 之类的东西切换行为 然后进行不同的配置。

相反的问题:如果我进行CLR35和CLR20配置,是否可以根据此选择目标平台? 该选项在 VS2008 中不可用,而且我对 MSBUILD 还不太了解。

Are there any predefined constants in the C# compiler to detect what version of the platform is being targeted?

I can set a platform in Project options but how can I write single source for .NET 2 and .NET 3.5 ?

Edit: I really want to write some code once and be able to switch behavior with something like #if CLR_VERSION35
and then rn different configurations.

The reverse question: If I make CLR35 and CLR20 configurations is it possible to select target platform based on this? The option is not available in VS2008 and I don't know a lot about MSBUILD yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流心雨 2024-07-21 19:45:32

.NET Framework 3.5 是 2.0 的超集,因此所有 2.0 应用程序都应在 3.5 上运行而无需修改。 由于它是一个超集,如果您的应用程序发现自己处于 2.0 版本,那么就没有办法不使用 3.5 功能,无论如何这都没有意义。

如果您需要以 2.0 为目标,并且可以在 2.0 中编写所需的内容,那么就可以了,3.5 仍然可以正常运行您的应用程序。 实际上,您必须编写最低公分母或在安装程序中包含 3.5 运行时可再发行组件,以确保您的客户拥有适用于您的应用程序的正确环境。

.NET Framework 3.5 is a superset of 2.0 so all 2.0 apps should run on 3.5 without modification. As it is a superset there's no way of back-peddling to not use 3.5 functionality if your app finds itself on 2.0, not that makes sense anyway.

If you need to target 2.0 and you can write what you need in 2.0 then that's the way to go, 3.5 will still run your app fine. Effectively, you have to write for the lowest common denominator or include the 3.5 runtime redistributables with your installer to ensure that your customer has the correct environment for your app.

在VS2008中您可以在项目属性中设置目标框架(解决方案资源管理器,右键单击项目)。 创建新项目时,您还可以选择,新项目窗口的右上角有一个下拉菜单。

In VS2008 you can set the target framework in the project properties (Solution explorer, right-click project). You also have the option when you create a new project, there's a drop-down in the top-right of the new project window.

从此见与不见 2024-07-21 19:45:32

据我所知,除了 DEBUG 和 RELEASE 之外没有其他的。 我将保存搜索并在项目属性“构建”选项卡“条件编译符号”中定义自己的搜索。

如果有任何用途,请参阅此博客了解更多信息

Aside from DEBUG and RELEASE there aren't as far as I know. I would save the searching and just define my own inside the project properties,"Build" tab, "Conditional compilation symbols".

There more information on this blog if that's any use

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文