![](/img/trans.png)
[英]How do compiler directives work with project.json in Visual Studio 2015
[英]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工具还不够完善,但可能会在不久的将来解决此错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.