I am using Microsoft Visual Studio Professional 2022 (64-bit), I am trying to add classes to a DLL. Previously I have created a DLL which contains string resources and this works fine. However any attempt at adding a class, even the simplest example, crashes. Here is my demo DLL class:
namespace SimonP {
public class clsTest {
public static void ShowName() {
Console.WriteLine("Hello World");
}
}
}
This is the project (.sln
) file:
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34525.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimonP", "SimonP.csproj", "{A483645B-2014-47B0-BD80-DA56F4356D3F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A483645B-2014-47B0-BD80-DA56F4356D3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A483645B-2014-47B0-BD80-DA56F4356D3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A483645B-2014-47B0-BD80-DA56F4356D3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A483645B-2014-47B0-BD80-DA56F4356D3F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C307CDC2-0CE5-46C5-9B0F-5BD811779D98}
EndGlobalSection
EndGlobal
In my C# application, at the top of FrmMain.cs
:
using SimonP;
Later in the FrmMain
constructor:
public FrmMain() {
try {
SimonP.clsTest.ShowName();
} catch(FileNotFoundException ex) {
Console.WriteLine("HACK");
}
}
I've built this and am running it only in the debugger, but as soon as I start debugging I get:
System.IO.FileNotFoundException
HResult=0x80070002
Message=Could not load file or assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Source=Project
StackTrace:
at Project.FrmMain..ctor() in C:\LocalDev\VisualStudio\Project\FrmMain.cs:line 55
at Project.Program.Main() in C:\LocalDev\VisualStudio\Project\Program.cs:line 24
I've searched online for demos and examples and I really can't see what I've done wrong.
When running in debug I've tried to add a Watch to 'SimonP.clsTest.ShowName' the Value shows:
error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
I just create a simple Console project, added the reference to the DLL and the exact same call, it worked with no exceptions.
I then create another project this time a Windows Forms App (.NET Framework), added the reference to the dll and the same call to Form1.cs Form1() after InitializeComponet()
and I get:
System.IO.FileNotFoundException
HResult=0x80070002
Message=Could not load file or assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Source=SimonPApp
StackTrace:
at SimonPApp.Form1..ctor() in C:\LocalDev\VisualStudio\SimonP\SimonPApp\Form1.cs:line 17
at SimonPApp.Program.Main() in C:\LocalDev\VisualStudio\SimonP\SimonPApp\Program.cs:line 16
SimonPApp project properties, Application, Target framework:.NET Framework 4.7.2
Build Platform:Active (Any CPU)
Platform target:x64
Prefer 32-bit:unchecked
Your exception message reveals a hint to solving the problem:
System.IO.FileNotFoundException HResult=0x80070002 Message=Could not load file or assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
System.Runtime, Version=8.0.0.0
refers to a BCL assembly from .NET 8.0, which the CLR is saying it can't find...
.exe
with the Program.Main()
method) is running in an older version of .NET and tries to load and run a .dll
file targeting a newer version of .NET..sln
file has this line: Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimonP", "SimonP.csproj"
SimonP.csproj
project file targets .NET Framework 4.x (because FAE04EC0...
is the well-known-GUID for .csproj
files predating the newer "SDK-style" .csproj
) and these project-files cannot reference .NET 8.SimonP.csproj
is likely targeting .NET Framework 4.x and trying to load a .dll
which targets .NET 8.0, and that's going to fail.SimonP.csproj
to an SDK-style project file and change the <TargetFramework>
to net8.0
..csproj
for your hitherto unnamed .dll
file to target .NET Framework 4.x or .NET Standard 2.0 instead of .NET 8.0.