简体   繁体   中英

Targetting multiple .net framework versions by using different project configurations

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:

  1. 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.

  2. Edit each project file in a text editor (not VS). Find all the PropertyGroup elements that refer to the new .Net35 configuration, eg:

     <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.

  3. 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, eg:

     <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.

I do not think you can do that with multiple configuration of the same project.

You can do that, though, by having multiple project files, each one targeting a different framework version.

EDIT: The easiest way to do that, btw is to just copy your current project file, open it, and change the targeted framework version.

You may not need to do this... it only takes a simple config file to make a .NET 3.5 application run against .NET 4, in theory. That could be an easier solution than using two separate builds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM