views:

68

answers:

3

I have a solution that contains several projects. One of the test projects is using .NET 4.0 due to a dependency. The deploy-able project I want to build is targeted against the .NET 3.5 compiler. Our current build script is not granular enough to target different .NET compilers on a project by project basis. 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?

Solution Structure

  • Project 1 .NET 3.5 (deploy-able)
  • Project 2 .NET 4.0 (used for testing in automated build cycle)

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.

Edit:

I am not compiling this code within Visual Studio. We have a custom build script that is run external to visual studio. It is only smart enough to use 1 .NET compiler. 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. Instead it compiles them with the defined compiler.

Project 1 does not reference project 2. Project 2 is a project containing unit tests. Our automated build tool compiles project 2 only to run tests.

+3  A: 

EDIT

Op clarified the question is the comments as

Can the 4.0 compiler compile a 3.5 project

The answer is yes this is very possible.

JaredPar
Nope, I mean we have a build script that compiles all projects in a solution. The build script can only target 1 .NET Compiler. So in the case it is targeting the .NET 4.0 compile for compilation of every project in the solution. At this point assemblies haven't been created.
sgmeyer
@sgmeyer, i'm still confused. Are you compiling everything twice?
JaredPar
@JaredPar no, I am not compiling everything twice. My build script runs through it once. I am asking if I can compile a 3.5 project with a 4.0 compiler. Not compile a project referencing a 3.5 assembly.
sgmeyer
@sgmeyer, then the answer is yes. This is very much doable.
JaredPar
@JaredPar Awesome! Thanks very much.
sgmeyer
+2  A: 

Right click on your "Project 1" project in the solution explorer. Next click Properties (hot key is ALT+Enter). Under the application tab there is a "Target Framework" drop down. Set that to 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. Just compile the solution file and every project will compile properly.

Bob
I am not compiling with Visual Studio. The first part does nothing for me. Instead I am compiling with a build script external to Visual Studio. This is controlled by our build team. I am wondering if this is possible without modifying the build script.
sgmeyer
Are you developing in Visual Studio? If so do what I said, it will update your project file. Then use MSBuild to compile your solution file, it will pick up what framework version to build each project with based on their setting from the project file.
Bob
I am developing in VS2010. It is already targeted at 3.5. I can compile everything just fine in VS. The problem is with with the build server and the scripts it uses to build the projects. That is why I wanted to know if .NET 4.0 C# compiler will compiler .NET 3.5 projects.
sgmeyer
Technically none of the Microsoft C# compilers can compile any .NET project file (.csproj). The C# Compiler can compile C# source code files (.cs) and they can target different versions of the .NET framework for the output binary. MSBuild can compile project files (.csproj) and solution files (.sln). So the answer is No. You can do what you want however by using MSBuild and specifing the target framework in the project properties.
Bob
+1 Thanks for the clarification on MSBuild vs .NET compiler.
sgmeyer
A: 

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.

If you are doing something esoteric like compiling NHibernate source code as part of your solution you will run into problems. Otherwise, if you aren't stepping on any namespaces you shouldn't have a problem compiling a 3.5 project to 4.0.

What is wrong with taking Bob's answer? Are you using a custom build solution (i.e. not Visual Studio)?

wllmsaccnt