简体   繁体   English

使用不同的项目配置来定位多个.net框架版本

[英]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. 一个配置将针对.net框架3.5,另一个配置将针对.net框架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. 我创建了一个名为DotNet35的新配置(使用通常的步骤),它将以.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. 我已经通过将创建的项目配置中的目标版本指定为v3.5来完成此操作。它似乎不起作用。 Any idea why? 知道为什么吗? Here is the property group section from my .csproj (only manual addition is the TargetFrameworkVersion element) 这是我的.csproj中的属性组部分(只有手动添加是TargetFrameworkVersion元素)

<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. 你所做的应该有效,尽管Visual Studio会在你的项目设置中显示错误的“目标框架”。

In my experience it is possible to target multiple .NET framework versions in a single project, by modifying your project file (.csproj) manually. 根据我的经验,可以通过手动修改项目文件(.csproj)在单个项目中定位多个.NET框架版本。

Suppose you already have a .NET 4 configuration and you want to create a .NET 3.5 configuration. 假设您已经拥有.NET 4配置,并且想要创建.NET 3.5配置。 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. 在Visual Studio中,根据现有的“调试”和“发布”配置,创建两个新的解决方案配置(在Configuration Manager中),其名称如“Debug.Net35”和“Release.Net35”。 Tell VS to "Create new project configurations". 告诉VS“创建新项目配置”。 Then save your projects and exit Visual Studio. 然后保存您的项目并退出Visual Studio。

  2. Edit each project file in a text editor (not VS). 在文本编辑器(而不是VS)中编辑每个项目文件。 Find all the PropertyGroup elements that refer to the new .Net35 configuration, eg: 查找引用新.Net35配置的所有PropertyGroup元素,例如:

     <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release.Net35|AnyCPU'"> 

    Add a TargetFrameworkVersion element below that line: 在该行下面添加一个TargetFrameworkVersion元素:

     <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> 

    Remember : there are usually 2-4 PropertyGroups that you will have to change. 请记住 :您通常需要更改2-4个PropertyGroup。 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. 为了更好地衡量,您可能还想为原始配置添加<TargetFrameworkVersion> ,但这是可选的,因为原始<TargetFrameworkVersion>将从非限定<ProjectGroup>元素继承。

  3. You may need to add references that exist in one .NET framework version but not another. 您可能需要添加一个.NET框架版本中存在的引用,而不是另一个。 For example you might use System.Core.dll, but only in .NET 3.5 and later, and WindowsBase.dll except in .NET 2.0. 例如,您可能使用System.Core.dll,但仅限于.NET 3.5及更高版本,以及WindowsBase.dll,但.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: 或者您可以使用Theraot的.NET兼容性DLL ,但仅限于.NET 3.5及更早版本,例如:

     <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 . 我相信ItemGroup元素应该是Project的子元素,而不是现有ItemGroup的子元素。

Visual Studio will behave a little strangely. Visual Studio会表现得有些奇怪。 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. 例如,当您使用“添加引用”时,列出的引用将基于默认的 .NET框架版本,而不是当前配置指定的当前版本。

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. 您可能不需要这样做......理论上,它只需要一个简单的配置文件来使.NET 3.5应用程序针对.NET 4运行。 That could be an easier solution than using two separate builds. 这可能比使用两个单独的构建更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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