如何使用 csproj 多目标 .NET Core 类库? [英] How do you multi-target a .NET Core class library with csproj?

查看:37
本文介绍了如何使用 csproj 多目标 .NET Core 类库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 .NET Core 仍然使用 project.json 格式时,您可以构建一个类库

解决方案

需要手动编辑项目文件,将s添加到默认的TargetFramework中,基本改目标框架.然后你用 提及 Moniker;强> 分隔符.

您也可以手动或使用 VS Nuget 包管理器将 Nuget 包引用放入条件项组中.

您的 .csproj 应该如下所示:

<属性组><TargetFrameworks>netstandard1.6;net452</TargetFrameworks></PropertyGroup><ItemGroup Condition="'$(TargetFramework)' == 'net452'"><PackageReference Include="Microsoft.Azure.DocumentDB"><版本>1.12.0</版本></PackageReference></项目组><ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.6'"><PackageReference Include="Microsoft.Azure.DocumentDB.Core"><版本>1.1.0</版本></PackageReference></项目组></项目>

我这几天因为缺少文档而做的另一个解决方法是,我在 VS2015 中创建一个项目并使用可用文档和智能感知形成 project.json,然后在 VS2017 中打开解决方案并使用内置升级.然后我将查看 csproj 文件以找出如何进行该配置.

多目标更深奥的目标,没有Moniker:

微软:

<块引用>

不推荐使用 PCL+

虽然支持 PCL,但包作者应该支持网络标准代替..NET 平台标准是PCLs 并使用单个表示跨平台的二进制可移植性不像便携式 a+b+c 名称那样与静态绑定的名称.

如果您想针对便携式配置文件,它没有预定义的名称,因此便携式配置文件也无法推断 TargetFrameworkIdentifierTargetFrameworkVersionTargetFrameworkProfile.此外,编译器常量不是自动定义的.最后,您必须添加所有程序集引用,默认情况下均不提供.

下面这个例子取自一个使用 dynamic 关键字的项目,因此它额外需要 Microsoft.CSharp 程序集,因此您可以看到它如何引用不同的目标.

<属性组><TargetFrameworks>netstandard1.5;net40;portable40-net45+sl5+win8+wp8</TargetFrameworks></PropertyGroup><PropertyGroup Condition="'$(TargetFramework)'=='portable40-net45+sl5+win8+wp8'"><TargetFrameworkIdentifier>.NETPortable</TargetFrameworkIdentifier><TargetFrameworkVersion>v4.0</TargetFrameworkVersion><TargetFrameworkProfile>Profile158</TargetFrameworkProfile><DefineConstants>$(DefineConstants);PORTABLE158</DefineConstants></PropertyGroup><ItemGroup Condition="'$(TargetFramework)'=='netstandard1.5'"><PackageReference Include="Microsoft.CSharp" Version="4.3.0"/><PackageReference Include="System.ComponentModel" Version="4.3.0"/></项目组><ItemGroup Condition="'$(TargetFramework)'=='net40'"><Reference Include="Microsoft.CSharp"/></项目组><ItemGroup Condition="'$(TargetFramework)'=='portable40-net45+sl5+win8+wp8'"><Reference Include="Microsoft.CSharp"/><Reference Include="系统"/><Reference Include="System.Core"/><Reference Include="System.Windows"/></项目组></项目>

When .NET Core still used the project.json format, you could build a class library targeting multiple frameworks (e.g. net451, netcoreapp1.0).

Now that the official project format is csproj using MSBuild, how do you specify multiple frameworks to target? I am trying to look for this from the project settings in VS2017, but I am able to only target a single framework from the .NET Core frameworks (it doesn't even list the other full .NET Framework versions which I do have installed):

解决方案

You need to manually edit the project file and add s to the default TargetFramework and basically change it to TargetFrameworks. Then you mention the Moniker with a ; separator.

Also you can put the Nuget package references in a conditional ItemGroup manually or using VS Nuget Package Manager.

Here is what your .csproj should look like:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>netstandard1.6;net452</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'net452'">
    <PackageReference Include="Microsoft.Azure.DocumentDB">
      <Version>1.12.0</Version>
    </PackageReference>
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.6'">
    <PackageReference Include="Microsoft.Azure.DocumentDB.Core">
    <Version>1.1.0</Version>
    </PackageReference>
  </ItemGroup>
</Project>

Another workaround I do these days because of missing documentation is that I create a project in VS2015 and form the project.json using the available documentation and intellisense, then open the solution in VS2017 and use the built-in upgrade. I will then look at the csproj file to figure out how to make that configuration happen.

Multi-targeting more esoteric targets without a Moniker:

Microsoft:

PCLs are not recommended+

Although PCLs are supported, package authors should support netstandard instead. The .NET Platform Standard is an evolution of PCLs and represents binary portability across platforms using a single moniker that isn't tied to a static like like portable-a+b+c monikers.

If you want to target a Portable Profile it doesn't have a predefined moniker so Portable Profiles also can't infer TargetFrameworkIdentifier, TargetFrameworkVersion, and TargetFrameworkProfile. Also a compiler constant isn't defined automatically. Finally you have to add all assembly references none are provided by default.

This Example below is taken from a project that used the dynamic keyword so it additionally needed the Microsoft.CSharp assembly, thus you can see how it's references for different targets.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>netstandard1.5;net40;portable40-net45+sl5+win8+wp8</TargetFrameworks>
  </PropertyGroup>

  <PropertyGroup Condition="'$(TargetFramework)'=='portable40-net45+sl5+win8+wp8'">
    <TargetFrameworkIdentifier>.NETPortable</TargetFrameworkIdentifier>
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <TargetFrameworkProfile>Profile158</TargetFrameworkProfile>
    <DefineConstants>$(DefineConstants);PORTABLE158</DefineConstants>
  </PropertyGroup>

  <ItemGroup Condition="'$(TargetFramework)'=='netstandard1.5'">
    <PackageReference Include="Microsoft.CSharp" Version="4.3.0" />
    <PackageReference Include="System.ComponentModel" Version="4.3.0" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)'=='net40'">
    <Reference Include="Microsoft.CSharp" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)'=='portable40-net45+sl5+win8+wp8'">
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Windows" />
  </ItemGroup>
</Project>

这篇关于如何使用 csproj 多目标 .NET Core 类库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆