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

标签 c# .net visual-studio visual-studio-2015 project.json

我正在创建一个面向多个平台(.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 工具还不够完善,但这个错误可能会在不久的将来得到解决。

关于c# - 如何让 Visual Studio 2015 xproject (project.json) 引用依赖项目的最高框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40709226/

相关文章:

c# - Windows 窗体将 List 传递给新窗体

c# PropertyGrid 限制对 List<T> 项的编辑

c# - 参数未从 $http.post 传递到 api Controller

c# - XElement 添加函数将 xmlns =""添加到 XElement

.net - 是否有用于 SQL 或 MySQL 等的 "object database"插件?

c# - 我可以在 ASP.NET Core 启动期间访问数据库吗?

visual-studio - 无法创建虚拟机

c++ - : error C2065: 'Lcurrent' : undeclared identifier

c++ - VS 2015 链接器错误,预编译 header

c# - UserControl 与模型的绑定(bind) - "property not found"