14 May, 2011

Same codebase –> multiple .NET frameworks ?

Building a logger what I want to be available to both .NET 3.5 and .NET 4.0 clients, I do not want to have two copies of the same projects (one for 3.5 and one for 4.0). So what to do? How can you setup Visual Studio to build and emit code for respectively 3.5 and 4.0 all from the same code base?

The short answer is: You can not! You have to have a physical copy of the project for each version, unless you hand-fiddle with the project (.csproj) files ;-) In that case, Yes – we can (to quote some politician that I hold in very positive esteem).

Build for 2 frameworks...

How to do?

1) In Visual Studio – you set 2 new Configurations (‘3.5’ and ‘4.0’)

Settings dialog

2) Close the project

3) Open the project file (.csproj) in an external editor (e.g. Notepad++)

4) Locate the ‘<TargetFrameworkVersion>’ tag in the file in the first PropertyGroup!


5) Take out this element/tag and insert it into the two new PropertyGroups just created in step 1


6) Replace the text with respectively (one for each property group)
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>

7) Close the file (.csproj) and re-open it in Visual Studio 2010

8) Now you can select the target configuration (3.5 or 4.0) and you will see an assembly built for the two different frameworks respectively


9) You are done!


To verify that this actually works, open the files (dll) in Reflector and see the internal references

.NET 3.5 version     .NET 4.0 version

How clever is that?

No comments:

iPhone/XCode - not all cases are equal!

This bit me! Having made some changes to an iPhone application (Obj-C); everything worked fine in the simulator. But, when deploying the s...