简体   繁体   English

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

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

I'm creating a reusable library that targets several platforms (.NET 4.0, .NET 4.5, .NETStandard 1.0 and .NETStandard 1.3). 我正在创建一个针对多个平台(.NET 4.0,.NET 4.5,.NETStandard 1.0和.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版本包含一些在.NET 4.0版本下不可用的功能。

The unit test project that references this library project has one single target platform, namely NET 4.5.1. 引用此库项目的单元测试项目具有一个单一的目标平台,即NET 4.5.1。 This test project obviously contains some code that tests the .NET 4.5 specific features of the core library. 该测试项目显然包含一些代码,这些代码测试核心库的.NET 4.5特定功能。

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. 但是不幸的是,测试项目无法编译,因为Visual Studio似乎引用了.NETStandard 1.0版本,该版本显然不包含此功能。

To demonstrate my problem, I reduced this to the following two projects: 为了演示我的问题,我将其简化为以下两个项目:

Core library: 核心库:

{
  "version": "1.0.0-*",

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

Code file: 代码文件:

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

#if NET40
    public class ClassNet40 { }
#endif

#if NET45
    public class ClassNet45 { }
#endif
}

Test library: 测试库:

{
  "version": "1.0.0-*",

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

Code: 码:

// 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();

What should I change to allow my unit test project to compile and test the specific .NET 4.5 features? 我应该更改什么以允许我的单元测试项目编译和测试特定的.NET 4.5功能?

There seems to be a bug in Visual Studio tools for .NET Core. .NET Core的Visual Studio工具中似乎存在一个错误。 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. 当您从另一个项目中引用多框架项目时-Visual Studio仅从列表中获取第一个列出的框架(在您的情况下为“ netstandard1.0”),并将引用的项目视为该框架的单个目标。 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. 另一方面,当您使用ClassNet45定义时- 似乎它没有编译(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. 似乎.NET Core的Visual Studio工具还不够完善,但可能会在不久的将来解决此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM