如何在NuGet中基于目标框架指定条件依赖项?

Tra*_*lig 13 nuget nuspec

我正在构建一个引用Microsoft CommonServiceLocator程序集的NuGet包.

那里有两个版本的Microsoft CommonServiceLocator:

我的项目是一个可移植类库,但由于它有时与Enterprise Library一起使用,我需要对"可移植版本"进行"有条件"引用,这样就没有冲突了.

  • 如果目标框架是完整的.NET 4.0/4.5,请使用原始的CommonServiceLocator包,以便人们也可以使用Enterprise Library位(也引用CommonServiceLocator包).
  • 如果目标框架是可移植的(或其他任何东西),请使用Portable.CommonServiceLocator包.

我在NuGet文档中看到了新的"组"功能,显示了如何在.nuspec文件中指定依赖项,我认为这将完成我想要的,但我不确定如何测试它.

这是我认为我需要做的事情,我希望有人可以验证我的方法或指出我正确的方向:

<dependencies>
  <group>
    <!-- Always include regardless of target framework -->
    <dependency id="Autofac" />
  </group>
  <group targetFramework="net40">
    <!-- Also include the full CSL if it's full framework -->
    <dependency id="CommonServiceLocator" />
  </group>
  <group targetFramework="portable-win+sl50+wp8">
    <!-- Otherwise include the Portable CSL -->
    <dependency id="Portable.CommonServiceLocator" />
  </group>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

特别...

  • 我的targetFramework语法合适吗?我找不到任何示例,所以我不知道+分隔机制是否正确或是否应该以逗号分隔.
  • 默认组是否有效?具有未指定目标框架的那个组 - 将始终包含在内还是我需要在每个组中复制/粘贴它?

Dee*_*pak 6

是的,这非常正确.有关可移植框架名称的详细信息,请访问http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package#Framework_Names

我发现还有一件事,因为win + sl50 + wp8默认包含net45,你可能想要包含它以便安装这个依赖组.