[英]How do I get a reference to a class library project to work?
Using the following 2 files:使用以下 2 个文件:
C# Class library project Target Framework: .NET v 5.0 C# Class 库项目目标框架:.NET v 5.0
using System;
namespace ClassLibrary1
{
public class MyExternalClass { }
}
C# console project .NET Framework: 4.8 C# 控制台项目 .NET 框架:4.8
using System;
using ClassLibrary1;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
MyExternalClass myObj = new MyExternalClass();
Console.WriteLine(myObj.ToString());
Console.ReadKey();
}
}
}
I get the following error when running the program System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.运行程序 System.IO.FileNotFoundException 时出现以下错误:'Could not load file or assembly'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一。 The system cannot find the file specified.'
该系统找不到指定的文件。'
Copied details: System.IO.FileNotFoundException HResult=0x80070002 Message=Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.复制的详细信息:System.IO.FileNotFoundException HResult=0x80070002 消息=无法加载文件或程序集“System.Runtime,版本=5.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a”或其依赖项之一。 The system cannot find the file specified.
该系统找不到指定的文件。 Source= StackTrace:
来源=堆栈跟踪:
I've added the reference for the library as normal, and I didn't use any additional packages from NuGet.我已经像往常一样添加了库的引用,并且我没有使用 NuGet 中的任何其他包。 This is as minimalist as I can get.
这是我能得到的极简主义。 This persists for every .NET target framework except for .NET Standard 2.0
除了 .NET 标准 2.0 之外,这对于每个 .NET 目标框架都存在
I've read some possible fixes involving manual edits of visual studio files but none have worked for me so far, granted that they're several years old and involve other editions and frameworks.我已经阅读了一些可能的修复,涉及手动编辑 Visual Studio 文件,但到目前为止,没有一个对我有用,因为它们已经有好几年了,并且涉及其他版本和框架。
In order to be able to reference a library from a .NET framework project the libary needs to target .NET Framework or .NET Standard.为了能够从 .NET 框架项目中引用库,该库需要以 .NET 框架或 .NET 标准为目标。
.NET 5 and .NET Framework are different beasts. .NET 5 和 .NET 框架是不同的野兽。
A good spot to see this is .NET Standard article:.看到这个的好地方是.NET 标准文章:。 You can see there that .NET and .NET Framework are separate rows in the table showing .NET versions.
您可以在那里看到 .NET 和 .NET Framework 是表格中显示 .NET 版本的单独行。
The answer to why "this persists for every .NET target framework except for .NET Standard 2.0" can be found in the in the same article:为什么“除了 .NET 标准 2.0 之外,每个 .NET 目标框架都存在这种情况”的答案可以在同一篇文章中找到:
To find the highest version of .NET Standard that you can target, do the following steps:
要查找您可以定位的 .NET 标准的最高版本,请执行以下步骤:
- (...)
(...)
- Repeat this process for each platform you want to target.
对您要定位的每个平台重复此过程。 If you have more than one target platform, you should pick the smaller version among them.
如果您有多个目标平台,则应选择其中较小的版本。 For example, if you want to run on .NET Framework 4.8 and .NET 5.0, the highest .NET Standard version you can use is .NET Standard 2.0.
例如,如果您要在 .NET Framework 4.8 和 .NET 5.0 上运行,那么您可以使用的最高 .NET Standard 版本是 Z3031BBBE.508EDB9072DAZ6 Standard。
Yes it can be confusing是的,这可能会令人困惑
In Introducing .NET 5 from May 2019 Richard Lander, program manager in .NET Team, writes:在 2019 年 5 月推出 .NET 5时,.NET 团队的项目经理 Richard Lander 写道:
.NET 5 = .NET Core vNext
.NET 5 = .NET 核心 vNext
.NET 5 is the next step forward with .NET Core.
.NET 5 是 .NET 核心的下一步。 (...) The project aims to improve .NET in a few key ways:
(...) 该项目旨在通过以下几个关键方式改进 .NET:
- Produce a single .NET runtime and framework that can be used everywhere and that has uniform runtime behaviors and developer experiences.
生成一个单一的 .NET 运行时和框架,可以在任何地方使用,并且具有统一的运行时行为和开发人员体验。
- Expand the capabilities of .NET by taking the best of .NET Core, .NET Framework, Xamarin and Mono.
Expand the capabilities of .NET by taking the best of .NET Core, .NET Framework, Xamarin and Mono.
- (...)
(...)
Later he writes:后来他写道:
We're skipping the version 4 because it would confuse users that are familiar with the .NET Framework, which has been using the 4.x series for a long time.
我们跳过第 4 版,因为它会使熟悉 .NET 框架的用户感到困惑,该框架一直在使用 4.x 系列。 Additionally, we wanted to clearly communicate that .NET 5 is the future for the .NET platform.
此外,我们想清楚地表明 .NET 5 是 .NET 平台的未来。
And finally:最后:
Check out .NET Core is the Future of .NET to understand how .NET 5 relates to .NET Framework.
查看.NET 核心是 .NET 的未来,了解 .NET 5 与 .NET 框架的关系。
The error is probably due to version mismatch or change after creating the projects.该错误可能是由于版本不匹配或创建项目后发生更改。
Recreate both projects targeting 4.8 version, it should solve the issue.重新创建两个针对 4.8 版本的项目,它应该可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.