如何让Visual Studio 2015 xproject (project.json) 引用依赖项目的最高框架

How to let a Visual Studio 2015 xproject (project.json) reference the highest framework of a depending project

我正在创建一个针对多个平台(.NET 4.0、.NET 4.5、.NETStandard 1.0 和 .NETStandard 1.3)的可重用库。该项目的.NET 4.5 版本包含一些.NET 4.0 版本下不可用的功能。

引用该库项目的单元测试项目只有一个目标平台,即 NET 4.5.1。这个测试项目显然包含一些测试核心库的 .NET 4.5 特定功能的代码。

不幸的是,测试项目没有编译通过,因为Visual Studio似乎引用了.NETStandard 1.0版本,显然不包含此功能。

为了演示我的问题,我将其简化为以下两个项目:

核心库:

{
  "version": "1.0.0-*",

  "frameworks": {
    "netstandard1.0": {
      "dependencies": {
        "NETStandard.Library": "1.6.0"
      }
    },
    "net40": {},
    "net45": {}
  }
}

代码文件:

namespace CoreLibrary
{
#if NETSTANDARD1_0
    public class ClassNetStandard { }
#endif

#if NET40
    public class ClassNet40 { }
#endif

#if NET45
    public class ClassNet45 { }
#endif
}

测试库:

{
  "version": "1.0.0-*",

  "dependencies": {
    "CoreLibrary": { "target": "project" }
  },
  "frameworks": {
    "net451": {}
  }
}

代码:

// This compiles
new CoreLibrary.ClassNetStandard();

// This doesn't.
// error: Type or namespace 'ClassNet40' does not exist in namespace 'CoreLibrary'
new CoreLibrary.ClassNet40();
// error: Type or namespace 'ClassNet45' does not exist in namespace 'CoreLibrary'
new CoreLibrary.ClassNet45();

我应该更改什么以使我的单元测试项目能够编译和测试特定的 .NET 4.5 功能?

.NET Core 的 Visual Studio 工具中似乎存在错误。当您从另一个项目引用多框架项目时 - Visual Studio 仅从列表中获取第一个列出的框架(在您的情况下 - "netstandard1.0")并将该引用项目视为该框架的单一目标。但是,编译器会正确处理此问题,虽然 似乎 该项目构建正确 - 但实际上并非如此。另一方面,当您使用 ClassNet45 定义时 - 它 似乎 它没有编译(Visual Studio 显示错误) - 它确实编译成功。

.NET Core 的 Visual Studio 工具似乎还不够完善,但这个错误可能会在不久的将来得到解决。