.NET多目标与.NET标准和可移植库MSBuild 15

jbt*_*ule 4 c# f# portable-class-library visual-studio-2017 msbuild-15

移植到.NET标准版本,您可能与可移植库相同,但支持的平台可能不是.

例如,.NET standard 1.6可能是最便宜的api可用的最低版本Profile47.Profile47支持.net 4.5或更高版本,nut .NET Standard 1.6仅支持4.6.1及更高版本!

使用新的msbuild 15 csproj/fsproj (也是VS2017)进行新的多目标定位是否可以同时编译可移植库.Net标准以使转换更容易?

jbt*_*ule 6

是的,但它并不像交叉编译那样明显.netstandard带有.net45.net40作为不容易的绰号为您的便携式库中预定义.

在新的sdk的Microsoft.NET.TargetFrameworkInference.targets中它说:

对于不支持推理的情况,可以如下明确指定标识符,版本和配置文件:

   <PropertyGroup>
     <TargetFrameworks>portable-net451+win81;xyz1.0</TargetFrameworks>
   <PropertyGroup>
   <PropertyGroup Condition="'$(TargetFramework)' == 'portable-net451+win81'">
     <TargetFrameworkIdentifier>.NETPortable</TargetFrameworkIdentifier>
     <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
     <TargetFrameworkProfile>Profile44</TargetFrameworkProfile>
   </PropertyGroup>
   <PropertyGroup Condition="'$(TargetFramework)' == 'xyz1.0'">
     <TargetFrameworkIdentifier>Xyz</TargetFrameworkVersion>
   <PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

TargetFrameworkProfile你可以从旧的csproj/fsproj中获得.这是您的便携式库的配置文件标识符.您也可以从Nuget Target Frameworks中查找它.

TargetFrameworkIdentifier很简单,就是这样.NETPortable

TargetFrameworkProfile也许可以从旧的csproj/fsprog中获取.但你也可以检查C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\和检查v4.0,v4.5以及v4.6个人资料目录,找到你的个人资料.这些目录的列表如下:

V4.0

Profile1/    Profile143/  Profile2/    Profile3/    Profile4/   Profile6/
Profile102/  Profile147/  Profile225/  Profile328/  Profile41/  Profile88/
Profile104/  Profile154/  Profile23/   Profile336/  Profile42/  Profile92/
Profile131/  Profile158/  Profile24/   Profile344/  Profile46/  Profile95/
Profile136/  Profile18/   Profile240/  Profile36/   Profile47/  Profile96/
Profile14/   Profile19/   Profile255/  Profile37/   Profile5/
Run Code Online (Sandbox Code Playgroud)

V4.5

Profile111/  Profile259/  Profile49/  Profile7/  Profile75/  Profile78/
Run Code Online (Sandbox Code Playgroud)

V4.6

Profile151/  Profile157/  Profile31/  Profile32/  Profile44/  Profile84/
Run Code Online (Sandbox Code Playgroud)

您为便携式目标选择的绰号是什么?是和否,重要的是自动包装nuget,因此最好使用Nuget Target Frameworks中的值,需要注意一点.我不确定是不是因为最新版本的nuget,但它似乎想要将TargetFrameworkVersion添加到portable绰号中.因此Profile47,v4.0实际应该是portable40-net45+sl5+win8

添加编译器的另一个项定义了使用DefineConstants可能会发现哪些有用.你可以把它做成任何你想要的东西,它在历史上一直是大写的PROFILEXXX,只需将它添加到条件组.

<DefineConstants>$(DefineConstants);PROFILE47</DefineConstants>
Run Code Online (Sandbox Code Playgroud)

最后要注意的是,默认的Reference包括没有提供,所以你必须添加自己的.

<ItemGroup Condition="'$(TargetFramework)'=='portable40-net45+sl5+win8'">
  <Reference Include="System" />
  <Reference Include="System.Core" />
  <Reference Include="System.Windows" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

CSharp示例

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>netstandard1.6;portable40-net45+sl5+win8</TargetFrameworks>
    <Description>YOUR DESCRIPTION</Description>
    <Company>YOUR COMPANY</Company>
    <Authors> YOUR AUTHORS </Authors>
    <Copyright>YOUR COPYRIGHT</Copyright>
    <AssemblyVersion>YOUR VERSION</AssemblyVersion>
    <FileVersion>YOUR VERSION</FileVersion>
    <PackageProjectUrl>YOUR PROJECT URL</PackageProjectUrl>
    <PackageLicenseUrl>YOUR LICENSE</PackageLicenseUrl>
    <PackageTags>YOUR TAGS</PackageTags>
    <IncludeSymbols>True</IncludeSymbols>
    <IncludeSource>True</IncludeSource>
    <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
    <PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
    <Version>YOUR NUGET VERSION</Version>
  </PropertyGroup>

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

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

</Project>
Run Code Online (Sandbox Code Playgroud)

F#示例

*警告:F#需要测试版的Mono for Mac或Visual Studio 2017预览15.3目前这个fsproj.此外,对示例的更改包括PackageReference FSharp.Compiler.ToolsFSharp.Core4.1最后一个可移植支持的FSharp版本(这是相对较新的版本).

<Project Sdk="FSharp.NET.Sdk;Microsoft.NET.Sdk" ToolsVersion="15.0">

  <PropertyGroup>
    <TargetFrameworks>netstandard1.6;portable40-net45+sl5+win8</TargetFrameworks>
    <Description>YOUR DESCRIPTION</Description>
    <Company>YOUR COMPANY</Company>
    <Authors> YOUR AUTHORS </Authors>
    <Copyright>YOUR COPYRIGHT</Copyright>
    <AssemblyVersion>YOUR VERSION</AssemblyVersion>
    <FileVersion>YOUR VERSION</FileVersion>
    <PackageProjectUrl>YOUR PROJECT URL</PackageProjectUrl>
    <PackageLicenseUrl>YOUR LICENSE</PackageLicenseUrl>
    <PackageTags>YOUR TAGS</PackageTags>
    <IncludeSymbols>True</IncludeSymbols>
    <IncludeSource>True</IncludeSource>
    <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
    <PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
    <Version>YOUR NUGET VERSION</Version>
  </PropertyGroup>

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

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

  <ItemGroup>
    <PackageReference Include="FSharp.Core" Version="4.1.*" />
    <PackageReference Include="FSharp.Compiler.Tools" Version="4.1.*" PrivateAssets="All" 
    />
    <PackageReference Include="FSharp.NET.Sdk" Version="1.0.*" PrivateAssets="All" 
    />
  </ItemGroup>

  <ItemGroup>
    <Compile Include="YourModule1.fs" />
    <Compile Include="YourModule2.fs" />
  </ItemGroup>

</Project>
Run Code Online (Sandbox Code Playgroud)