简体   繁体   English

使用.NET 4.0编译器编译.NET 3.5项目

[英]Compiling .NET 3.5 project using .NET 4.0 compiler

I have a solution that contains several projects. 我有一个包含几个项目的解决方案。 One of the test projects is using .NET 4.0 due to a dependency. 由于存在依赖性,测试项目之一正在使用.NET 4.0。 The deploy-able project I want to build is targeted against the .NET 3.5 compiler. 我要构建的可部署项目针对的是.NET 3.5编译器。 Our current build script is not granular enough to target different .NET compilers on a project by project basis. 我们当前的构建脚本不够细致,无法逐个项目地针对不同的.NET编译器。 Basically I can only use one compiler for every project in the solution. 基本上,我只能为解决方案中的每个项目使用一个编译器。

Is the .NET 4.0 compiler smart enough to compile 3.5 code? .NET 4.0编译器是否足够智能以编译3.5代码?

Solution Structure 解决方案结构

  • Project 1 .NET 3.5 (deploy-able) Project 1 .NET 3.5(可部署)
  • Project 2 .NET 4.0 (used for testing in automated build cycle) Project 2 .NET 4.0(用于自动构建周期中的测试)

Note: I can not just ignore Project 2, because our automated build server runs the unit tests in project 2. All tests must pass before a build is successful. 注意:我不能仅仅忽略Project 2,因为我们的自动构建服务器在项目2中运行单元测试。所有测试必须在构建成功之前通过。

Edit: 编辑:

I am not compiling this code within Visual Studio. 我不在Visual Studio中编译此代码。 We have a custom build script that is run external to visual studio. 我们有一个自定义生成脚本,该脚本在Visual Studio外部运行。 It is only smart enough to use 1 .NET compiler. 仅使用1 .NET编译器足够聪明。 It uses this compiler to compile all of the projects in a given solution. 它使用此编译器来编译给定解决方案中的所有项目。 The build script does not care whether or not the projects are defined to be compiled against 3.5 or 4.0. 生成脚本不在乎是否将项目定义为针对3.5或4.0进行编译。 Instead it compiles them with the defined compiler. 而是使用定义的编译器编译它们。

Project 1 does not reference project 2. Project 2 is a project containing unit tests. 项目1不引用项目2。项目2是包含单元测试的项目。 Our automated build tool compiles project 2 only to run tests. 我们的自动化构建工具仅编译项目2即可运行测试。

EDIT 编辑

Op clarified the question is the comments as Op澄清了问题是评论为

Can the 4.0 compiler compile a 3.5 project 4.0编译器可以编译3.5项目吗

The answer is yes this is very possible. 答案是可以的。

Right click on your "Project 1" project in the solution explorer. 在解决方案资源管理器中右键单击“ Project 1”项目。 Next click Properties (hot key is ALT+Enter). 接下来单击属性(热键为ALT + Enter)。 Under the application tab there is a "Target Framework" drop down. 在应用程序选项卡下,有一个“目标框架”下拉列表。 Set that to 3.5. 将其设置为3.5。

Now when you compile (it sounds like you are using a command line) use MSBuild and don't specify a target framework version. 现在,当您编译(听起来像您在使用命令行)时,请使用MSBuild ,而不指定目标框架版本。 Just compile the solution file and every project will compile properly. 只需编译解决方案文件,每个项目都将正确编译。

You may run into issues depending on the types of libraries you are using. 根据所使用的库的类型,您可能会遇到问题。 In particular I had trouble with namespaces/class names that didn't exist in in 3.5 that were implemented in 4.0. 特别是,我遇到了在4.0中实现的3.5中不存在的名称空间/类名称的问题。

If you are doing something esoteric like compiling NHibernate source code as part of your solution you will run into problems. 如果您正在做一些深奥的事情,例如将NHibernate源代码编译为解决方案的一部分,则会遇到问题。 Otherwise, if you aren't stepping on any namespaces you shouldn't have a problem compiling a 3.5 project to 4.0. 否则,如果您不使用任何名称空间,那么将3.5项目编译为4.0时就不会有问题。

What is wrong with taking Bob's answer? 接受鲍勃的答案有什么问题? Are you using a custom build solution (ie not Visual Studio)? 您是否正在使用自定义生成解决方案(即,不是Visual Studio)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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