简体   繁体   English

从 .Net 标准类库引用 .Net Core 3.1 项目

[英]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.

相关问题 从.NET核心项目引用可移植类库 - Referencing a Portable Class Library from a .NET Core Project 从Windows类库引用.NET标准库 - Referencing a .NET Standard library from a Windows Class Library 引用.NET Core XUnit项目中的标准dll - Referencing standard dlls from a .NET Core XUnit project 从类库(Windows)引用.NET Core类库 - Referencing .NET Core Class Library from Class Library (Windows) 从.NET Core应用程序引用.NET 4.6.2类库 - Referencing .NET 4.6.2 class library from .NET Core app 如何从ASP.NET Core读取.NET标准类库项目中的连接字符串 - How to read connection string inside .NET Standard Class library project from ASP.NET Core ASP.NET Core 3.1 API 参考 a.Net Standard class 库 - 在 Azure DevOps 中构建失败 - ASP.NET Core 3.1 API referring to a .Net Standard class library - build failed in Azure DevOps 问题:在.Net Core 3.1 中引用.Net Standard 2.1 项目 - Issue: Referring .Net Standard 2.1 project in .Net Core 3.1 从.NET Core 1.0类库引用mscorlib 4.0.0.0 - Referencing mscorlib 4.0.0.0 from .NET Core 1.0 class library 从.net核心控制台应用程序引用.net标准2.0库中的dll时出错 - Error referencing dll in .net standard 2.0 library from .net core console application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM