简体   繁体   中英

Targeting multiple versions of .net framework

Suppose I have some code that would, in theory, compile against any version of the .net framework. Think "Hello World", if you like.

If I actually compile the code, though, I'll get an executable that runs against one particular version.

Is there any way to arrange things so that the compiled exe will just run against whatever version it finds? I strongly suspect that the answer is no, but I'd be happy to be proven wrong...


Edit: Well, I'll go to the foot of our stairs. I had no idea that later frameworks would happily run exe's compiled under earlier versions. Thanks for all the responses!

我不确定这是否正确,但我会尝试编译它为最低版本,更高版本应该能够运行较低版本的exe。

Read ScuttGu's post about VS 2008 Multi-Targeting Support

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.

That way you can use VS2008 to develop .NET 2.0 projects that will work on both .NET 2.0, 3.0 and 3.5

沿着多目标,框架是向后兼容的,因此编译为1.0的内容将在1.1和2上运行。在1.1上编译的内容将在2 ......上运行。

I know @John Boker is correct when it comes to .Net class libraries. You can compile a class library against .Net 1.1 and then use it in a .Net 2.0 or higher project.

I suspect the same is also true for executables.

with 2005 & 2008, yes (on CLR 2.0)

With 2003, no.. because it compiles down to CLR 1.1

You could theorectically write some code using #if (DOTNET35) and such so that you don't use features outside the compilers knowledge and then run the desired compiler on the app... I question the usefulness of this though.

Well, AFAIK, all .NET versions (except version 1.x) compile to the same bytecode. In case of C#, all new features are simply syntactic sugar, which get transformed into C# 2.0 constructs when compiling. The key point where things could go wrong is when you use C# 3.0 or 3.5 specific DLLs. They don't work well with the .NET 2.0 framework, so you can't use those.

I can't really think of a workaround for this, sorry :(

On the subject of which .NET framework the user has installed, there is also a new option with the Client Profile that's available with .NET 3.5 SP1. This basically allows you to ship a small (277k) bootstrap program which downloads and installs the required files (A subset od the full .NET framework).

For more information, and general tips on creating a small .NET installation, see this great blog entry by Scott Hanselman .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM