How do I get a reference to a class library project to work?

0

Using the following 2 files:

C# Class library project Target Framework: .NET v 5.0

using System;

namespace ClassLibrary1
{
    public class MyExternalClass { }
}

C# console project .NET Framework: 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. 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. 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. This is as minimalist as I can get. This persists for every .NET target framework except for .NET Standard 2.0

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.

c#
visual-studio-2019
.net-5
.net-4.8
asked on Stack Overflow Apr 6, 2021 by Max Jambon • edited Apr 6, 2021 by ADyson

2 Answers

2

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 5 and .NET Framework are different beasts.

A good spot to see this is .NET Standard article:. You can see there that .NET and .NET Framework are separate rows in the table showing .NET versions.

enter image description here

.NET Standard 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:

To find the highest version of .NET Standard that you can target, do the following steps:

  • (...)
  • 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.

4.8 and 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:

.NET 5 = .NET Core vNext

.NET 5 is the next step forward with .NET Core. (...) The project aims to improve .NET in a few key ways:

  • Produce a single .NET runtime and framework that can be used everywhere and that has uniform runtime behaviors and developer experiences.
  • 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. Additionally, we wanted to clearly communicate that .NET 5 is the future for the .NET platform.

And finally:

Check out .NET Core is the Future of .NET to understand how .NET 5 relates to .NET Framework.

answered on Stack Overflow Apr 7, 2021 by tymtam
-1

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.

answered on Stack Overflow Apr 7, 2021 by SantoshBogarajula

User contributions licensed under CC BY-SA 3.0