0

我正在通过TargetFrameworks开发一个针对 net35 (.NET Framework 3.5)、netstandard2.0 (.NET Standard 2.0) 和 netcoreapp3.1 (.NET Core 3.1) 的库。

对于其中一项功能,该库在内部使用WPF,因此此功能仅在面向 net35 (.NET Framework 3.5) 和 netcoreapp3.1 (.NET Core 3.1) 时有效。

我使用 MSBuild包目标为我的库创建 NuGet 包,但由于 WPF 用于 netcoreapp3.1,我还为 netcoreapp3.1 创建参考程序集,以便非 Windows 平台上的消费者在构建项目时使用该参考程序集,如解释的那样在这个PackageReference 支持部分。

我的问题是:如何指示 MSBuild包目标在创建的 NuGet 包的 'ref/' 文件夹中包含参考程序集?

4

1 回答 1

2

我还在NuGet 的文档 GitHub上问了这个问题,我得到的答案是没有对此的本机支持,所以我实现了一个解决方法,并将其发布在这里供其他人使用:

<!-- Generate also reference assembly. -->
<!-- See: -->
<!-- https://docs.microsoft.com/en-us/dotnet/standard/assembly/reference-assemblies#generating-reference-assemblies -->
<!-- https://github.com/dotnet/roslyn/blob/master/docs/features/refout.md -->
<PropertyGroup>
  <ProduceReferenceAssembly>true</ProduceReferenceAssembly>
</PropertyGroup>

<!-- This is a workaround until better support for \ref (Reference Assembly) is added in Pack target. -->
<!-- See: -->
<!-- https://docs.microsoft.com/en-us/nuget/create-packages/select-assemblies-referenced-by-projects#packagereference-support -->
<!-- Need a away to specify \ref (Reference Assembly) as target folder in Pack target (https://github.com/NuGet/Home/issues/4184) -->
<PropertyGroup>
  <!-- Supress warning NU5131 (https://docs.microsoft.com/en-us/nuget/reference/errors-and-warnings/nu5131) which is reported because .nuspec file is not updated -->
  <!-- to contain <references> element (https://docs.microsoft.com/en-us/nuget/reference/nuspec#explicit-assembly-references) for all files inside the 'ref\' directory.  -->
  <NoWarn>$(NoWarn),NU5131</NoWarn>
  <!-- See https://docs.microsoft.com/en-us/nuget/reference/msbuild-targets#targetsfortfmspecificcontentinpackage -->
  <TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);AddRefAssemblyToPackage</TargetsForTfmSpecificContentInPackage>
</PropertyGroup>
<Target Name="AddRefAssemblyToPackage">
  <!-- Add reference assembly and XML documentation to 'ref/'. -->
  <ItemGroup Condition=" Exists('$(BaseOutputPath)$(Configuration)\$(TargetFramework)\ref\$(AssemblyName).dll') ">
    <TfmSpecificPackageFile Include="$(BaseOutputPath)$(Configuration)\$(TargetFramework)\ref\$(AssemblyName).dll" PackagePath="ref/$(TargetFramework)" />
    <TfmSpecificPackageFile Include="$(BaseOutputPath)$(Configuration)\$(TargetFramework)\$(AssemblyName).xml" PackagePath="ref/$(TargetFramework)" />
  </ItemGroup>
</Target>
于 2021-01-20T08:31:46.157 回答