[英]Referencing a .Net Core 3.1 project from a .Net Standard Class Library
So I have a .NET Core 3.1
project.所以我有一个
.NET Core 3.1
项目。 I am trying to create a unit testing project to go along with it.我正在尝试创建一个单元测试项目来配合它。 I have created a
.NET Standard 2.1 Class Library
for this purpose.为此,我创建了一个
.NET Standard 2.1 Class Library
。 I'm then trying to add a reference to the main project, in order to be able to reference code from there.然后我尝试添加对主项目的引用,以便能够从那里引用代码。 This isn't working, as I get the following compilation errors:
这不起作用,因为我收到以下编译错误:
'..\\MyProj\\MyProj.csproj' targets 'netcoreapp3.1'.
“..\\MyProj\\MyProj.csproj”针对“netcoreapp3.1”。 It cannot be referenced by a project that targets '.NETStandard,Version=v2.1'.
它不能被以“.NETStandard,Version=v2.1”为目标的项目引用。 Project MyProj is not compatible with netstandard2.1 (.NETStandard,Version=v2.1).
Project MyProj 与 netstandard2.1 (.NETStandard,Version=v2.1) 不兼容。 Project MyProj supports: netcoreapp3.1 (.NETCoreApp,Version=v3.1) Test (test\\Test)
项目 MyProj 支持:netcoreapp3.1 (.NETCoreApp,Version=v3.1) 测试 (test\\Test)
So this does make sense to me, as I understand why .NET Core
wouldn't be able to reference a .NET Standard
project.所以这对我来说确实有意义,因为我理解为什么
.NET Core
无法引用.NET Standard
项目。
But then how can I create a Unit Testing project?但是,我该如何创建单元测试项目呢? Is it possible to create a
.NET Core 3.1 Class Library
?是否可以创建
.NET Core 3.1 Class Library
? From everything I have read, it seems like that doesn't exist.从我读过的所有内容来看,这似乎不存在。
How can I set up a unit test project that references the main project?如何设置引用主项目的单元测试项目?
.NET Standard was created as an interface for different .NET implementations. .NET Standard 是作为不同 .NET 实现的接口创建的。 For example, this allows you to share code between .NET Core and the full .NET framework.
例如,这允许您在 .NET Core 和完整的 .NET 框架之间共享代码。 I would create your library as a .NET Standard 2.0 project and then you can reference it in your .NET Core 3.1 application and your unit testing project.
我会将您的库创建为 .NET Standard 2.0 项目,然后您可以在您的 .NET Core 3.1 应用程序和单元测试项目中引用它。
I only suggest .NET Standard as it is generally more portable than .NET Framework or .NET Core libraries.我只建议使用 .NET Standard,因为它通常比 .NET Framework 或 .NET Core 库更具可移植性。 You'd be able to target more platforms such as Xamarin, desktop, or web with .NET Standard.
你将能够使用 .NET Standard 以更多平台为目标,例如 Xamarin、桌面或 Web。
I think you just need to use .Net Core 3.1 in your project, not NetStandard.我认为您只需要在您的项目中使用 .Net Core 3.1,而不是 NetStandard。 I use it this way and there is no troubles.
我是这样使用的,没有任何问题。 I guess you don't really need your unit testing to be portable?
我想你真的不需要你的单元测试是可移植的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.