为什么我无法在 ASP.NET 2.0 中使用对象初始值设定项?
为什么我可以在面向 .NET 2.0 的 Visual Studio 2008 Windows 项目等中使用对象初始值设定项
,但不能在面向 .NET 2.0 的 ASP.NET 项目中使用?
我知道这是 C# 3.0 功能,但不明白 - 为什么这可以在 .NET 2.0 项目中使用。
Why I can use Object Initializers
in Visual Studio 2008 Windows projects, etc targeted to .NET 2.0 but cannot - in ASP.NET projects targeted to .NET 2.0 ?
I understand that this is C# 3.0 features, but don't - why this possible to use in .NET 2.0 projects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
可能是因为针对 2.0 框架的 ASP.Net 内容假设它将在可能必须动态编译一些代码的模式下运行。 由于它在 2.0 模式下运行,因此它将在该阶段获得 2.0 编译器(因此任何依赖于 3.0 编译器的东西都会失败)
当从“正常”项目定位 2.0 代码库时,编译就完成了,因此依赖 3.0 编译器功能很好。
Probably because the ASP.Net stuff targeting the 2.0 framework assumes it will run in a mode where it may have to compile some code on the fly. Since it is running in 2.0 mode it will get the 2.0 compiler at that stage (thus anything relying on the 3.0 compiler will fail)
When targeting the 2.0 codebase from a 'normal' project the compilation is done then and there so relying on 3.0 compiler functionality is fine.
当您以 .NET 2.0 运行时为目标时,您也以 C# 2.0 编译器为目标。 该版本的编译器不理解 3.0 的功能。
When you target the .NET 2.0 runtime, you are also targeting the C# 2.0 compiler. That version of the compiler does not understand the 3.0 features.
这是一个现有的问题 涉及在 .NET 2.0 项目中使用 C# 3.0 功能。 它解释了哪些 C# 3.0 功能可用、哪些不可用以及如何使用它们。
为了帮助您解决任何特定的困难,我们需要具体的细节。
Here's an existing question that deals with using C# 3.0 features in .NET 2.0 projects. It explains which C# 3.0 features are available, which aren't and how to use them.
In order to help you with any specific difficulty, we'll need specific details.
有关 C# 高级功能以及它们如何从 C# 1.0 发展到 3.0 的最佳信息来源是 乔恩·斯基特的书。 他经常出现在这里,甚至可能提供一些额外的见解。 然而,你确实应该拥有这本书。
The best source of information on advanced featured in C# and how they have evolved from C# 1.0 through 3.0 is Jon Skeet's book. He is here on SO quite often and may even offer some additional insight. However, you really should have the book.
普通的 ASP.NET 网站会动态编译,这意味着它将使用 Web 服务器上的虚拟目录可用的编译器。 如果设置为 ASP.NET 2.0,它将使用 2.0 编译器(而不是针对 .NET 2.0 的 3.0 编译器)。
我假设当您将网站转换为 Web 应用程序时,您将能够定位 3.0 并仍然使用一些 3.0 功能。 我建议尝试一下,我愿意,但我现在没有时间。 让我们知道它是否有效;-)
A normal ASP.NET Website compiles on the fly, which means it will use the compiler available to that virtual directory on your web server. If it's set to ASP.NET 2.0, it will use the 2.0 compiler (not the 3.0 compiler targeted to .NET 2.0).
I assume when you convert your website to a web application you WILL be able to target 3.0 and still use some of the 3.0 features. I suggest trying that, I would, but I don't have the time at the moment. Let us know if it works ;-)