I have a class library that targets .NET 3.5. Now, I would like to add some features that require .NET 4.0, but I still want to be able to generate a version that targets .NET 3.5 (without these features of course).
Ideally, I would like to target a different framework version based on the configuration:
Unfortunately it doesn't seem to be possible, as the target framework is global for the whole project...
Is there a way around it? I know I could create a second project that includes the same files, but it's pretty bad for maintainability....
What is the easiest way to do this? If you have ever done something similar, which approach worked best?
First of all, you could start off by creating a conditional compilation symbol that enables you to include or exclude the extra features that are only available for the .NET4.0 framework.
Next to that, I think you'll have to use MSBuild directly, instead of letting VS.NET build your project. I've done something similar once. In short, it comes down to the fact that you'll have to create 2 build tasks within your MSbuild script. One that allows you to build your project for .NET 3.5, and one that allows you to build the project targetting .NET 4.0.
In each build-task, you can define the target framework, and the conditional compilation symbol that you'd want to use.
The build - tasks in your build-script could like like this:
<Target Name="buildall-v4">
<!-- The following ItemGroup defines all the build-constants that have to be used
in the build.
As can be seen, the DEBUG & RELEASE constants are only included when necessary -->
<ItemGroup>
<BuildConstant Include="DEBUG" Condition="'$(buildmode)'=='DEBUG'" />
<BuildConstant Include="RELEASE" Condition="'$(buildmode)'=='RELEASE'" />
<BuildConstant Include="NET_FRAMEWORK_4_0" />
</ItemGroup>
<PropertyGroup>
<BuildConstantsToUse>@(BuildConstant)</BuildConstantsToUse>
</PropertyGroup>
<MSBuild Projects="$(builddir)\Source\MyProject.sln"
Properties="OutputPath=$(outputdir)\v4;Configuration=$(buildmode);DefineConstants=$(BuildConstantsToUse);TargetFrameworkVersion=v4.0" />
</Target>
<Target Name="buildall-v3.5">
<!-- The following ItemGroup defines all the build-constants that have to be used
in the build.
As can be seen, the DEBUG & RELEASE constants are only included when necessary -->
<ItemGroup>
<BuildConstant Include="DEBUG" Condition="'$(buildmode)'=='DEBUG'" />
<BuildConstant Include="RELEASE" Condition="'$(buildmode)'=='RELEASE'" />
</ItemGroup>
<PropertyGroup>
<BuildConstantsToUse>@(BuildConstant)</BuildConstantsToUse>
</PropertyGroup>
<MSBuild Projects="$(builddir)\Source\MyProject.sln"
Properties="OutputPath=$(outputdir)\v3.5\;Configuration=$(buildmode);DefineConstants=$(BuildConstantsToUse);TargetFrameworkVersion=v3.5" />
</Target>
Offcourse, when you'd want to build for the 2 versions, you'll have to execute each msbuild command separatly on the command line.
I learned how to do this using:
http://blogs.msdn.com/b/visualstudio/archive/2010/05/14/a-guide-to-vcxproj-and-props-file-structure.aspx
and
http://shazwazza.com/post/multi-targeting-a-single-net-project-to-build-for-different-framework-versions/
Basically, follow these steps:
Next:
Remove
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
from within the
<PropertyGroup Label="Globals">
and
</PropertyGroup>
tags.
Now:
Paste
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
or
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
within the appropriate "Debug"
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
or "Debug 4.0"
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug 4.0|Win32'" Label="Configuration">
<TargetFrameworkVersion Blah="blah blah">
tag in its PropertyGroup.Save the changes (may need to close the file as well if are using the same instance of visual studio where the project will be loaded).
Load or Reload the project in visual studio and verify that individual configurations build with different Frameworks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With