如何让Visual Studio 2015 xproject(project.json)引用依赖项目的最高框架 [英] How to let a Visual Studio 2015 xproject (project.json) reference the highest framework of a depending project

查看:117
本文介绍了如何让Visual Studio 2015 xproject(project.json)引用依赖项目的最高框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating a reusable library that targets several platforms (.NET 4.0, .NET 4.5, .NETStandard 1.0 and .NETStandard 1.3). The .NET 4.5 version of this project contains some features that are not available under the .NET 4.0 version.

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

The unit test project that references this library project has one single target platform, namely NET 4.5.1. This test project obviously contains some code that tests the .NET 4.5 specific features of the core library.

但是,不幸的是,该测试项目无法编译,因为Visual Studio似乎可以请参考.NETStandard 1.0版本,该版本显然不包含此功能。

Unfortunately however, the test project does not compile, because Visual Studio seems to reference the .NETStandard 1.0 version, which obviously does not contain this feature.

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

To demonstrate my problem, I reduced this to the following two projects:

核心库:

{
  "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功能?

What should I change to allow my unit test project to compile and test the specific .NET 4.5 features?

推荐答案

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

There seems to be a bug in Visual Studio tools for .NET Core. When you reference multi-framework project from another one - Visual Studio only takes first listed framework from a list (in your case - "netstandard1.0") and treats that referenced project as single targeted for this framework. However, compiler handles this correctly, and while it seems that project builds correctly - in reality it does not. On the other hand, when you use ClassNet45 definition - it seems that it does not compile (Visual Studio shows errors) - it really does compile successfully.

似乎.NET Core的Visual Studio工具还不够完善,但是可能不久以后就可以解决此错误。

It seems that Visual Studio tools for .NET Core are not polished enough yet, but probably this bug will be resolved in some near future.

这篇关于如何让Visual Studio 2015 xproject(project.json)引用依赖项目的最高框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆