【问题标题】:Would like to compile a basic program with a reference to a dll in C#想用 C# 中的 dll 来编译一个基本程序
【发布时间】:2021-01-31 20:38:47
【问题描述】:

我想编译一个简单的控制台应用程序,它使用来自 .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?)

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

【问题讨论】:

  • 尝试使用csc /r:library.dll /out:test.exe test.cs 并确保该库与test.cs 文件位于同一文件夹中
  • 我认为这可能有效.../r-reference 相同吗?我没有看到任何迹象表明 docs.microsoft.com/en-us/dotnet/csharp/language-reference/… 中可以使用反斜杠
  • 是的,一样的:docs.microsoft.com/en-us/dotnet/csharp/language-reference/…查看备注部分。我认为您调用中的问题是顺序,您在代码文件后添加了引用,csc 对参数顺序有点挑剔。
  • 但是它在哪里说正斜杠是可以的?此外,如果您想发布您的命令作为最终答案,我会接受...
  • 正斜杠和破折号都可以作为参数指示符。

标签: c# compiler-errors csc


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多