I have a project which i need to build in two different configurations. One configuration will target .net framework 3.5 and another will target .net framework 4.0. First of all is that possible? I have created a new configuration called DotNet35 (using the usual steps) which will target .net 3.5. I have done this by specifying the target version in the created project config as v3.5 It does not seem to work. Any idea why? Here is the property group section from my .csproj (only manual addition is the TargetFrameworkVersion element)
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DotNet35|AnyCPU'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\DotNet35\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<PlatformTarget>AnyCPU</PlatformTarget>
<CodeAnalysisLogFile>..\..\bin\Client\Debug\CS.XRAY.XRayClient.dll.CodeAnalysisLog.xml</CodeAnalysisLogFile>
<CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRuleSetDirectories>;C:\Program Files\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\\Rule Sets</CodeAnalysisRuleSetDirectories>
<CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisRuleDirectories>;C:\Program Files\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules</CodeAnalysisRuleDirectories>
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
</PropertyGroup>
What you did should have worked, although Visual Studio will display the wrong "Target framework" in your project settings.
In my experience it is possible to target multiple .NET framework versions in a single project, by modifying your project file (.csproj) manually.
Suppose you already have a .NET 4 configuration and you want to create a .NET 3.5 configuration. Do this:
In Visual Studio, create two new solution configurations (in Configuration Manager) with names such as "Debug.Net35" and "Release.Net35" based on your existing "Debug" and "Release" configurations. Tell VS to "Create new project configurations". Then save your projects and exit Visual Studio.
Edit each project file in a text editor (not VS). Find all the PropertyGroup elements that refer to the new .Net35 configuration, e.g.:
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release.Net35|AnyCPU'">
Add a TargetFrameworkVersion element below that line:
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
Remember: there are usually 2-4 PropertyGroups that you will have to change. For good measure, you might want to also add <TargetFrameworkVersion> for the original configurations, but this is optional since the original <TargetFrameworkVersion> will be inherited from the unqualified <ProjectGroup> element.
You may need to add references that exist in one .NET framework version but not another. For example you might use System.Core.dll, but only in .NET 3.5 and later, and WindowsBase.dll except in .NET 2.0. You can do this by creating special references in the project file:
<ItemGroup Condition="'$(TargetFrameworkVersion)'!='v2.0'">
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkVersion)'!='v2.0' And '$(TargetFrameworkVersion)'!='v3.0'">
<Reference Include="System.Core" />
</ItemGroup>
Or you might use Theraot's .NET compatibility DLL, but only in .NET 3.5 and earlier, e.g.:
<ItemGroup Condition="'$(TargetFrameworkVersion)'=='v3.5' Or '$(TargetFrameworkVersion)'=='v3.0' Or '$(TargetFrameworkVersion)'=='v2.0'">
<Reference Include="Theraot.Core">
<HintPath>..\Lib\Theraot.Core.dll</HintPath>
</Reference>
</ItemGroup>
I believe the ItemGroup element should be a child of Project, not a child of an existing ItemGroup.
Visual Studio will behave a little strangely. As you change configurations, the "Target framework" displayed in your project settings will always stay the same. For example, when you use "Add Reference", the references listed will be based on the default .NET framework version, not the current version specified by the current configuration.
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