英文:
Can not call a function from a C# DLL
问题
I created a DLL in C#, and try to call a function from it. But it has an error:
> 未经处理的异常: System.IO.FileNotFoundException: 找不到文件或程序集 "System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 或它的某一个依赖项。系统找不到指定的文件。
My DLL code:
using System;
namespace Draf_Lib
{
public class Class1
{
public static int add(int x, int y) { return x + y; }
}
}
And here is my C# code to call the function from it:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
//using MacroLib;
using Draf_Lib;
namespace Draft1
{
internal class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Hello");
Class1 c = new Class1();
int b = Class1.add(9, 8);
Console.WriteLine(b);
}
}
}
我已经将库包含为参考。有人能建议我如何修复它吗?
英文:
I created a DLL in C#, and try to call a function from it. But it has an error:
> Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
My DLL code:
using System;
namespace Draf_Lib
{
public class Class1
{
public static int add(int x, int y) { return x + y; }
}
}
And here is my C# code to call the function from it:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
//using MacroLib;
using Draf_Lib;
namespace Draft1
{
internal class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Hello");
Class1 c = new Class1();
int b = Class1.add(9, 8);
Console.WriteLine(b);
}
}
}
I already included the library as a reference. Can anyone suggest me a way to fix it?
答案1
得分: 1
这是因为在创建项目时选择了错误的目标框架。以下是我复现您的问题的示例:
-
首先,在创建类库时,我选择了“Class Library”,并选择了目标框架为.NET 6.0。
-
然后,我创建了一个控制台应用程序(.NET Framework)并选择了目标框架为.NET Framework 4.7.2。
-
当我引入dll并运行项目时,出现了以下错误。
实际上,当创建控制台程序时,您应该选择“Console App”,如下所示。目标框架可以选择.NET 6.0。
英文:
This is because you chose the wrong target framework when creating the project. Here's an example where I reproduce your problem:
- First, I selected Class Library when creating the class library, and the target framework was selected as .net 6.0
- Then I created a Console App (.net framework) and selected the target framework as .net framework 4.7.2
- The following error was reported when I introduced the dll and ran the project.
In fact, you should choose Console App when creating a console program, as follows. The target framework can choose .net 6.0.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论