Targeting Multiple Framework

问题描述 投票:0回答:1

到目前为止,我正在根据下载的框架使该标准库成为目标。例如,对于.NET Core,它将下载用于.NET Core的软件包

 <!--Trageting .NET CORE (2.1, 3.0) && .NET STANDARD (2.0,2.1) -->
  <ItemGroup Condition="$(TargetFramework.StartsWith('netcore')) OR $(TargetFramework.StartsWith('netstandard')) ">
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
    <!--<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.7" />-->
    <PackageReference Include="NLog.Extensions.Logging" Version="1.6.1" />
    <PackageReference Include="NLog.WindowsEventLog" Version="4.6.7" />
    <PackageReference Include="System.Diagnostics.EventLog" Version="4.6.0" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.6.0" />
    <PackageReference Include="System.Data.SqlClient" Version="4.7.0" />
    <PackageReference Include="System.Linq" Version="4.3.0" />
    <PackageReference Include="Microsoft.Win32.Registry" Version="4.6.0" />
  </ItemGroup>

  <!--Targeting .NET FRAMEWORK (4.6.1 4.6.2)  -->
  <ItemGroup Condition="'$(TargetFramework)' == 'net461' OR '$(TargetFramework)' == 'net462'">
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
    <PackageReference Include="Microsoft.AspNet.WebApi.Client" version="5.2.3" />
    <PackageReference Include="Microsoft.AspNet.WebApi.Core" version="5.2.3" />
    <PackageReference Include="NLog.Extensions.Logging" Version="1.5.4" />
    <PackageReference Include="NLog.WindowsEventLog" Version="4.6.7" />
    <PackageReference Include="System.Diagnostics.EventLog" Version="4.5.0" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
    <PackageReference Include="System.Data.SqlClient" Version="4.6.1" />
    <PackageReference Include="System.Linq" Version="4.3.0" />
    <PackageReference Include="Microsoft.Win32.Registry" Version="4.5.0" />
  </ItemGroup>

   <!--Targeting .NET FRAMEWORK (4.7, 4.7.1, 4.7.2)-->
  <ItemGroup Condition="'$(TargetFramework)' == 'net47' &#xD;&#xA; OR '$(TargetFramework)' == 'net471'&#xD;&#xA; OR '$(TargetFramework)' == 'net472'">
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
    <PackageReference Include="Microsoft.AspNet.WebApi.Client" version="5.2.3" />
    <PackageReference Include="Microsoft.AspNet.WebApi.Core" version="5.2.3" />
    <PackageReference Include="NLog.Extensions.Logging" Version="1.5.4" />
    <PackageReference Include="NLog.WindowsEventLog" Version="4.6.7" />
    <PackageReference Include="System.Diagnostics.EventLog" Version="4.5.0" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
    <PackageReference Include="System.Data.SqlClient" Version="4.6.1" />
    <PackageReference Include="System.Linq" Version="4.3.0" />
    <PackageReference Include="Microsoft.Win32.Registry" Version="4.5.0" />
  </ItemGroup>

是否有更好的方法可以执行上述代码,如使用DefineConstants所述

c# .net visual-studio core csproj
1个回答
0
投票

如果您要更改的是versions >>,并且它们具有一定的共性,那么我可能会想做类似的事情:

<PropertyGroup>
    <VersionWebApiClient>5.2.3</VersionWebApiClient>
    <VersionWebApiClient Condition="...todo...">5.2.7</VersionWebApiClient>
    <!-- ... for the other things ... -->
<PropertyGroup>
<ItemGroup>
    <PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="$(VersionWebApiClient)" />
</ItemGroup>

此:

  1. 允许最常见的值成为简单的默认值
  2. 允许清除每个目标框架的替代
  3. 不重复实际的软件包引用
© www.soinside.com 2019 - 2024. All rights reserved.