面向.net框架的多个版本
假设我有一些代码,理论上可以针对任何版本的.net框架进行编译。如果愿意,可以考虑" Hello World"。
但是,如果我实际编译代码,则会得到一个针对一个特定版本运行的可执行文件。
有什么方法可以安排事情,以便编译后的exe可以直接在找到的任何版本上运行?我强烈怀疑答案是否定的,但我很乐意被证明是错误的...
编辑:好吧,我去楼梯脚下。我不知道以后的框架会愉快地运行在早期版本下编译的exe。感谢所有回复!
解决方案
回答
阅读ScuttGu关于VS 2008多目标支持的文章
One of the big changes we are making starting with the VS 2008 release is to support what we call "Multi-Targeting" - which means that Visual Studio will now support targeting multiple versions of the .NET Framework, and developers will be able to start taking advantage of the new features Visual Studio provides without having to always upgrade their existing projects and deployed applications to use a new version of the .NET Framework library. Now when you open an existing project or create a new one with VS 2008, you can pick which version of the .NET Framework to work with - and the IDE will update its compilers and feature-set to match this. Among other things, this means that features, controls, projects, item-templates, and assembly references that don't work with that version of the framework will be hidden, and when you build your application you'll be able to take the compiled output and copy it onto a machine that only has an older version of the .NET Framework installed, and you'll know that the application will work.
这样,我们可以使用VS2008开发可在.NET 2.0、3.0和3.5上运行的.NET 2.0项目。
回答
我不确定这是否正确,但我会尝试将其编译为最低版本,较高版本应能够运行较低版本的exe。
回答
除了多重目标外,这些框架都是向后兼容的,因此编译为1.0的东西将在1.1和2上运行。在1.1编译的东西将在2 ...上运行等等。
回答
我知道@John Boker在.Net类库方面是正确的。我们可以针对.Net 1.1编译类库,然后在.Net 2.0或者更高版本的项目中使用它。
我怀疑对于可执行文件也是如此。
回答
对于2005和2008,是(在CLR 2.0上)
对于2003,否。因为它可以编译为CLR 1.1
我们可以从理论上使用#if(DOTNET35)编写一些代码,这样我们就不会使用编译器知识之外的功能,然后在应用程序上运行所需的编译器...我对此表示怀疑。
回答
好吧,AFAIK,所有.NET版本(版本1.x除外)都可以编译为相同的字节码。对于C#,所有新功能都只是语法糖,在编译时会转换为C2.0构造。
可能出问题的关键点是使用C3.0或者3.5特定DLL时。它们不能与.NET 2.0框架一起很好地工作,因此我们不能使用它们。
我真的想不出解决办法,对不起:(
回答
关于用户安装了哪个.NET框架,客户端配置文件中还有一个新选项,.NET 3.5 SP1中提供了该选项。这基本上使我们可以运送一个小的(277k)引导程序,该程序下载并安装所需的文件(完整.NET框架的子集)。
有关更多信息,以及有关创建小型.NET安装的一般提示,请参阅Scott Hanselman撰写的精彩博客文章。