使用不同的项目配置来针对多个 .NET Framework 版本进行定位

3

我有一个项目需要以两种不同的配置进行构建。其中一种配置将面向 .net framework 3.5,另一种则面向 .net framework 4.0。 首先,这是否可行? 我已经创建了一个名为 DotNet35 的新配置(使用通常的步骤),它将面向 .net 3.5。我通过在创建的项目配置中指定目标版本为 v3.5 来完成此操作。 但似乎并没有起作用。有什么想法吗?以下是我的 .csproj 中的 property group 部分 (仅手动添加了 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>
3个回答

7

你所做的应该可以生效,不过Visual Studio将在你的项目设置中显示错误的“目标框架”。

据我的经验,在单个项目中可以手动修改项目文件(.csproj)以针对多个.NET框架版本进行定位。

假设您已经拥有一个.NET 4配置,并且您想要创建一个.NET 3.5配置,请按照以下步骤操作:

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

  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, 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会有一些奇怪的行为。当您更改配置时,项目设置中显示的“目标框架”始终保持不变。例如,当您使用“添加引用”时,列出的引用将基于默认.NET框架版本,而不是当前配置指定的当前版本。


4

我不认为你可以通过同一项目的多个配置来完成此操作。

但是,您可以通过拥有多个项目文件来实现,每个项目文件针对不同的框架版本。

编辑:顺便说一句,最简单的方法是复制当前的项目文件,打开它,并更改目标框架版本。


1
将以下与编程有关的内容从英语翻译成中文。仅返回翻译后的文本,并添加原始代码文件作为参考。在一个解决方案下有2个项目。 - EvAlex

-1
你可能不需要这样做... 理论上只需要一个简单的配置文件就可以让.NET 3.5应用程序运行在.NET 4上。这可能比使用两个独立的构建更容易解决问题。

这应该是一个注释。 - julealgon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接