首页 > 解决方案 > 想用 C# 中的 dll 来编译一个基本程序

问题描述

我想编译一个简单的控制台应用程序,它使用来自 .dll 的引用来了解 csharp 中的基本动态链接是如何工作的。主要文件是test.cs

using System;
using TestLibrary;

namespace Test
{
    class Test
    {
        static int Main()
        {
            Console.WriteLine(TestLibrary.AddThreeNumbers(1,2,3));
            return 0;
        }
  
    }

}

类库是:

using System;

namespace TestLibrary
{
    class TestLibrary
    {
        int AddThreeNumbers(int a, int b, int c)
        {
            return a+b+c;
        }
  
    }

}

我在 powershell 开发人员命令提示符中使用以下内容编译library.cs成 dll:

csc library.cs -target:library -out:library.dll

这工作正常。但我随后尝试链接以生成可执行文件,如下所示:

csc test.cs -reference:TestLibrary=library.dll

但这会返回一个错误:

test.cs(2,7): error CS0246: The type or namespace name 'TestLibrary' could not be found (are you missing a using directive or an assembly reference?)

这是我的代码有问题还是我的编译方式有问题?任何帮助将非常感激。

标签: c#compiler-errorscsc

解决方案


正如@Gusman 所说,使用的命令是:

csc /r:library.dll /out:test.exe test.cs

推荐阅读