Step1: Unload the referencing project targeting .NET 2.0
Step2: Right click the unloaded project and select edit from context menu
Step3: Add <SpecificVersion>true</SpecificVersion>
to the reference. Below is a sample from my repro solution:
<ProjectReference Include="..\HighFX\HighFX.csproj">
<Project>{8DD71CAF-BEF7-40ED-9DD0-25033CD8009D}</Project>
<Name>HighFX</Name>
<SpecificVersion>true</SpecificVersion>
</ProjectReference>
Step4: Reload the project.
Now your should be able to build within the Visual Studio 2010, there could still be a warning as below, but the build can be successful.
Source: http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/dfadfb34-5328-4c53-8274-931c6ae00836
As Jon Skeet wrote, there is no apparent way to define the target framework at the solution level. However, this can be done by adding specific configurations to the solution, then editing the *.csproj
files manually to specify the targets for every configuration, rather than globally for the whole project.
Here is a step-by-step guide:
- Select Build - Configuration Manager.
- In the Active solution configuration drop-down list, select and enter a meaningful name (e.g.
Debug v3.5
) based on whatever existing settings you already have.
- For each project file, open it in the editor (you can do this in Visual Studio, but you have to first right-click on it in the Solution Explorer and select Unload Project, then Edit xyz.csproj).
- Find the
<PropertyGroup>
element which matches the configuration you have just created (for instance Debug v3.5|AnyCPU
).
- Add in the XML, for instance after the closing
</PlatformTarget>
element, the <TargetFrameworkVersion>
and <TargetFrameworkProfile>
you need.
- Save the file.
- In the Solution Explorer, use the command Reload Project to make the project available again.
Here is a sample:
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug v3.5|AnyCPU'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\Debug v3.5\</OutputPath>
<DefineConstants>DEBUG;TRACE;DOTNET35</DefineConstants>
...
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
...
</PropertyGroup>
Note that I also define the symbol DOTNET35
, which allows me to write framework dependent #if
statements in the source code. I had, for instance, a few pieces of code which relied on System.Tuple
and by adding a minimalistic version of the class, I could back-port my .NET 4.0 application to .NET 3.5.
Here is my snippet:
#if DOTNET35
namespace System
{
public class Tuple<T1, T2>
{
public Tuple(T1 item1, T2 item2)
{
this.item1 = item1;
this.item2 = item2;
}
public T1 Item1
{
get
{
return this.item1;
}
}
public T2 Item2
{
get
{
return this.item2;
}
}
private readonly T1 item1;
private readonly T2 item2;
}
}
#endif
Best Answer
This is mainly for future me, who will at some point come back to this question, and none of the current answers will solve your problem (hey future me!)
My issue was a nuget package referencing System.Core 2.0.5.0 so I had no control over the reference.
To get System.Core 2.0.5.0 installed on the machine, which already had .NET 4.0 installed and patched, I installed the Portable Class Library.