我正在创建一个针对多个平台(.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": {}
}
}
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();
我应该更改什么才能允许我的单元测试项目编译和测试特定的 .NET 4.5 功能?