VProj
is a simple tool that will copy an existing VS solution and convert it to build under the .NET 3.5 framework.
If you are building a re-usable library and you wish to target multiple .NET Framework versions (4.0 and 3.5) then you might find it useful.
Many projects that target multiple framework versions use a different approach, in that they add conditional elements in the MSBUILD project file. I have experimented with this alternative approach and prefer it for the following reasons:
- Minor changes to the Visual Studio projects are required, and they can be performed from Visual Studio.
- Less confusion to Visual Studio, as there are no custom edits to the project file.
- Plays nicer with nuget, as there are no conditional elements for assembly references.
That last point is an important one for me. I find that if I have hand-edited <Reference>
elements in my project file, it can confuse nuget during updates.
- Only knows about C# projects. It will ignore everything else. (I only use C#, but it would not be difficult to upgrade for VB, F#, etc).
- Does not convert Web projects. I find that I only want to use this for re-usable assemblies, I never want to target a web application for multiple frameworks.
- Only works for .NET 4.0 at the moment. In the near future I will probably have to enhance it to handle .NET 4.5, 4.0 and 3.5. (I don't intend to include .NET 3.0 or 2.0 at the moment).
Open your solution and decide which projects you wish to target for multiple frameworks. For each project, ensure that the compile constant NET40
is defined. It is important to define NET40
for every configuration in each project (eg Debug and Release for each project). If you do not define
NET40
for every configuration in a project, then that project will be ignored.
Run the vproj
tool to create a copy of your solution file and assocated project files.
To build for multiple framework targets, build your original (.NET 4.0) solution, and then build your copied (.NET 3.5) solution.
If you have code that needs conditional compilation, use the NET40
and NET35
conditional compilation constants:
#if NET40
// ... code that works under .NET 4.0
#endif
#if NET35
// ... code that works under .NET 3.5
#endif
C:\>vproj --help
Usage: vproj [ options ]
Convert a VS project from .NET 4.0 to .NET 3.5.
Options:
-p, --project=VALUE * Project file
-o, --output=VALUE Output file
-n, --nochange Show proposed changes, but do not write files
--clean Remove files created from previous operation
--noprompt Do not prompt to remove files, just do it
-v, --verbose Log additional debug messages
-q, --quiet Quiet operation, log errors and warnings only
-?, --help Show this help message
(Items marked * are mandatory)
The following steps outline how this utility converts the solution from .NET 4.0 to .NET 3.5
Changes to the solution file (eg MyProject.sln
):
-
Load the solution file and discard any projects that are not C# projects, and any C# projects that do not have the
NET40
compile constant defined for all configurations. -
Rename each project by adding ".Net35" to the end, eg
MyProject
becomesMyProject.Net35
. -
Rename each project file. So
src\MyProject.csproj
would becomesrc\MyProject.Net35.csproj
. -
Generate a new Project Guid for the project.
-
Update project dependencies to reflect project guid changes.
-
Save solution file to new file. So
MyProject.sln
would be saved toMyProject.Net35.sln
.
Changes to each project file:
-
Name changed and Guid changed as per above.
-
Remove compile constants
NET40
,NET40_CLIENT
. -
Add compile constant
NET35
-
Remove reference assemblies that are .NET 4.0 only. Currently this is
Microsoft.CSharp
, but there may be others. -
Change project references to reflect change of Guids and change of names.
-
Change assembly references. This uses the nuget convention. If the assembly hint contains
net40
ornet40-client
, then look for a matching assembly in the associatednet35
directory. -
Change the target framework version to v3.5.
-
Clear the target framework profile (ie if it is
Client
then it is changed to be empty).