使用 C# 3.0 编译的 C# 2.0 代码消耗程序集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# 2.0 code consuming assemblies compiled with C# 3.0
提问by Kev
This should be fine seeing as the CLR hasn't actually changed?
这应该没问题,因为 CLR 实际上并没有改变?
The boxes running the C# 2.0 code havehad .NET 3.5 rolled out.
运行 C# 2.0 代码的机器已经推出了 .NET 3.5。
The background is that we have a windows service (.NET 2.0 exe built with VS2005, deployed to ~150 servers) that dynamically loads assemblies (almost like plug-ins) to complete various work items asked of it. Whenever we roll out a new version of the bus logic, we just drop the assemblies on an FTP server and the windows service knows how to check for, grab and store the latest versions. New assemblies are now built using VS2008 and targetting .NET 2.0, we know that works ok. However we'd like to start taking advantage of C# 3.0 language features such as LINQ and targetting the assemblies against .NET 3.5 without having to build and deploy a new version of the windows service.
背景是我们有一个 Windows 服务(使用 VS2005 构建的 .NET 2.0 exe,部署到大约 150 个服务器),它动态加载程序集(几乎像插件一样)来完成要求它的各种工作项。每当我们推出新版本的总线逻辑时,我们只需将程序集放在 FTP 服务器上,Windows 服务就知道如何检查、获取和存储最新版本。新程序集现在使用 VS2008 构建并以 .NET 2.0 为目标,我们知道它可以正常工作。但是,我们希望开始利用 C# 3.0 语言功能(例如 LINQ)并将程序集定位到 .NET 3.5,而无需构建和部署新版本的 Windows 服务。
采纳答案by Keith
C#3 and .Net 3.5 adds new assemblies, but the IL is unchanged.
C#3 和 .Net 3.5 添加了新的程序集,但 IL 没有改变。
This means that with .Net 2 assemblies you can compile and use C#3, as long as you don't use Linq or anything else that references System.Linq or System.Core
这意味着使用 .Net 2 程序集,您可以编译和使用 C#3,只要您不使用 Linq 或任何其他引用 System.Linq 或 System.Core 的东西
yield
, var
, lambda syntax, anon types and initialisers are all compiler cleverness. The IL they produce is cross-compatible.
yield
, var
, lambda 语法,匿名类型和初始化程序都是编译器的聪明之处。他们生产的 IL 是交叉兼容的。
If you can reference the new assemblies for 3.5 it should all just work.
如果您可以引用 3.5 的新程序集,它应该都能正常工作。
There is no new version of ASP.Net - it should still be 2.0.50727 - but you should still compile for 3.5
没有新版本的 ASP.Net - 它应该仍然是 2.0.50727 - 但你仍然应该编译为 3.5
回答by lomaxx
This is interesting stuff. I was looking at LinqBridgeyesterday after someone on this forum suggested it to me and they are doing a similar thing.
这是有趣的东西。在这个论坛上有人向我建议之后,我昨天正在查看LinqBridge,他们正在做类似的事情。
I find it strange that Microsoft named the frameworks 2.0, 3.0 and 3.5 when they all compile down to produce the same IL required by the 2.0 CLR. I would have thought adding versions onto 2.0 would have made more sense altho I suppose it also is hard to get people to get their head around the fact that there are different versions of runtimes, compilers and languages.
我觉得奇怪的是,微软将这些框架命名为 2.0、3.0 和 3.5,因为它们都编译为 2.0 CLR 所需的相同 IL。我原以为将版本添加到 2.0 会更有意义,尽管我认为也很难让人们了解存在不同版本的运行时、编译器和语言的事实。
回答by Will Dean
yield, var, lambda syntax, anon types and initialisers are all compiler cleverness. The IL they produce is cross-compatible.
yield、var、lambda 语法、匿名类型和初始化程序都是编译器的聪明之处。他们生产的 IL 是交叉兼容的。
Minor nit-picking point, but yield was a 2.0 feature anyway.
小的挑剔点,但无论如何产量都是2.0的特性。