不能从C# DLL调用函数。

huangapple go评论145阅读模式
英文:

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?

不能从C# DLL调用函数。

答案1

得分: 1

这是因为在创建项目时选择了错误的目标框架。以下是我复现您的问题的示例:

  1. 首先,在创建类库时,我选择了“Class Library”,并选择了目标框架为.NET 6.0。

  2. 然后,我创建了一个控制台应用程序(.NET Framework)并选择了目标框架为.NET Framework 4.7.2。

  3. 当我引入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:

  1. First, I selected Class Library when creating the class library, and the target framework was selected as .net 6.0

不能从C# DLL调用函数。

不能从C# DLL调用函数。

  1. Then I created a Console App (.net framework) and selected the target framework as .net framework 4.7.2

不能从C# DLL调用函数。

  1. The following error was reported when I introduced the dll and ran the project.

不能从C# DLL调用函数。

In fact, you should choose Console App when creating a console program, as follows. The target framework can choose .net 6.0.

不能从C# DLL调用函数。

huangapple
  • 本文由 发表于 2023年6月13日 12:03:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定