VS2008 针对 .NET 2.0 并没有阻止我使用 C# 3 功能
我有一个 VS2005 解决方案,并想在新的一年中摆脱 VS2005。我对升级过程非常满意,并且惊喜地发现我的构建脚本大部分仍然有效。
我的问题是关于多目标功能 - 我的服务器上没有安装 .NET 3.5,因此我必须继续以 .NET 2.0 为目标。这大部分都有效,但我发现我可以做类似的事情
var returnMe = "result: " + result.ToString();
......并且仍然可以成功调试项目。
当我将该代码发送到构建服务器时,构建失败,提示“var”未定义。
那么,我应该期待哪个?
- VS2008,针对 .NET 2.0,当我尝试执行 C# 3 操作时,应该会抛出错误,
- 构建服务器意识到我的目标是 .NET 2.0,应该了解我在做什么并将其编译为 2.0 兼容的二进制文件
有什么想法吗?
I had a VS2005 solution and wanted to get off VS2005 for the new year. I was mostly happy with the upgrade process and pleasantly surprised to see that my build scripts mostly still worked.
My question is around the multi-targeting feature - I don't have .NET 3.5 installed on my servers, so I have to continue to target .NET 2.0. That worked, mostly, but I found that I could do things like
var returnMe = "result: " + result.ToString();
...and still debug the project successfully.
When I got that code up to the build server, the build failed, saying that "var" wasn't defined.
So, which should I be expecting?
- VS2008, targeting .NET 2.0, should throw errors when I try to do C# 3 things
- the build server, being aware that I'm targeting .NET 2.0, should understand what I'm doing and compile the thing into a 2.0-compatible binary
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题似乎是构建服务器没有正确的编译器。您可以在您的工作站上构建它,对吗?
许多 .NET 3.0“功能”只是在编译时恢复为 CLR 2 代码的语法位。
var
就是其中之一——编译时,编译器将 var 转换为适当的类型。在 http://weblogs.asp.net/shahar/archive/2008/01/23/use-c-3-features-from-c-2 -and-net-2-0-code.aspx
The problem appears to be that the build server does not have the right compiler. You can build it on your workstation, right?
Many of the .NET 3.0 "features" are just syntax bits that revert to CLR 2 code at compile time.
var
is one of them -- when it compiles, the compiler converts var to the appropriate type.There is a pretty informative post on this at http://weblogs.asp.net/shahar/archive/2008/01/23/use-c-3-features-from-c-2-and-net-2-0-code.aspx
当您使用 Visual Studio 2008 以 2.0 运行时为目标时,您仍然使用 3.0 版本的 C# 编译器来编译代码。从 2005 年到 2008 年,CLR 运行时本身并没有太大变化。它实际上只是核心 CLR 的一个服务包以及几个新库(特别是 System.Core.dll)的添加。
因此,2008 年添加的大部分功能在 2.0 运行时上运行得很好。这是完全合法的,如果您尝试使用在 2.0 中不起作用的功能,您将在构建时或发布时遇到错误。
When you use Visual Studio 2008 to target the 2.0 runtime, you are still using the 3.0 version of the C# compiler to compile your code. The CLR runtime itself did not change much between 2005 and 2008. It is in fact just a service pack to the core CLR and the addition of several new libraries (System.Core.dll in particular).
So the majority of the features added in 2008 work just fine on the 2.0 runtime. This is completely legal and if you attempt to use a feature which does not work in 2.0 you will encounter an error either at build time or publish time.
正如其他人所说,当使用 C# 3.0 编译器针对 .NET 2.0 运行时时,可以使用 C# 3.0 功能。
与此相关的一个鲜为人知的事实是,如果提供您自己的 LINQ 实现,您甚至可以将 LINQ 与 .NET 2.0 一起使用。
下面是一个启用“select”和“where”运算符的示例:
如果您想在 .NET 2.0 上充分利用 LINQ,则可以将 Mono 版本的 System.Core 与您的应用程序一起提供。
As others have said, C# 3.0 features can be used when targeting the .NET 2.0 runtime with the C# 3.0 compiler.
A little known fact related to this is that you can even use LINQ with .NET 2.0 if provide your own LINQ implementation.
Here's an example that enables the 'select' and 'where' operators:
You could ship Mono's version of System.Core with your application if you want to make full use of LINQ on .NET 2.0.
如果您想针对特定的构建,您可以通过配置设置来完成此操作(这正是我刚刚为 70-536 考试阅读的内容,非常枯燥的东西!)。
在 app.exe.config 文件中添加类似以下内容,
然后您可以强制所有构建使用相同的版本,而不管机器如何,这应该可以帮助您强制调试也使用正确的版本。
If you want to target a specific build you can do this via configuration settings (which is exactly what I've just been reading up for 70-536 exam, very dry stuff!).
Add something like the following in your app.exe.config file
You can then force all your builds to use the same version regardless of the machine, should help you to force the debugging to use the correct version too.
如果您不使用以下库/技术:
那么您或多或少是安全的,尽管我建议无论如何都要进行彻底的测试。
您可以使用 .Net 3.0 功能,如
var
等,只是因为 VS2008 在涉及动态语法检查、IntelliSence 等方面并不关心框架的目标版本可能是其他人。据我所知,VS2010将会关注这些事情,这将阻止您在针对 2.0 框架版本时成功编译“var x = ...
”结构。If you don't use the following libraries/technologies:
then you are more or less on the safe side, although I would recommend thorough testing anyway.
You're able to use .Net 3.0 features like
var
etc. just because VS2008 doesn't care about target version of the framework when it comes to things like on-the-fly syntax checking, IntelliSence and probably others. And as far as I know VS2010 will pay attention on these things, which will prevent you from successfull compiling your "var x = ...
" constructions when targeting 2.0 framework version.