简体   繁体   English

如何获得对 class 库项目的引用才能工作?

[英]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 版本的单独行。

在此处输入图像描述

.NET Standard 2.0 .NET 标准2.0

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。

4.8 and 5.0. 4.8 和 5.0。 It's confusing.这很令人困惑。

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.

相关问题 如何公开由类库引用的类库,该类库随后作为项目引用包含在Visual Studio中? - How do I expose class libraries referenced by a class library, which is then included as a project reference in Visual Studio? 如何在.NET核心类库中引用Visual Studio共享项目 - How do I reference a Visual Studio Shared Project in a .NET Core Class Library 如何在关联类库中添加对Web应用程序项目的引用? - How do I add a reference to a Web Application Project in an Associated Class Library? 无法获得.NET Core NUnit测试项目对我的生产类库的引用 - Cannot get .NET Core NUnit test project's reference to my production class library to work 我如何获得像HTML敏捷性这样的外部库来在我的C#项目中工作? - How Do I Get External LIbrary Like Html Agility To Work In My C# Project? 如何在同一解决方案中从4.6.1项目引用多目标.NET Core类库? - How do I reference a multi-target .NET Core class library from a 4.6.1 project within the same solution? 如何在项目中引用Aspnet类库 - How to reference Aspnet class library in a project 如何在类库项目中引用xml文件 - How to reference xml file in Class library project 我需要做什么才能使此代码在可移植类库中工作? - What need I do to get this code to work in a Portable Class Library? 如何在 .net 标准库中获取对 ARCGISRuntime SystemLocationDataSource 的引用? - How do I get a reference to ARCGISRuntime SystemLocationDataSource in .net Standard Library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM