c# - 想用 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?)
这是我的代码有问题还是我的编译方式有问题?任何帮助将非常感激。
解决方案
正如@Gusman 所说,使用的命令是:
csc /r:library.dll /out:test.exe test.cs
推荐阅读
- flutter - 我是否需要为 Flutter 中的 Hive TypeAdapter 创建创建额外的模型?
- css - 为什么在某些浏览器上,背景动画会在前景框上出现故障?
- r - 计算 R 中数据框中的组之间共享 account_ID 的次数
- javascript - onmouseenter 闪烁提示
- python - 如何使用 Pyspark Dataframe 分配一行?
- java - 如何创建像电子游戏一样的 LootBox 系统
- javascript - 如何隐藏文本/ div 并用星号替换它?
- image - Octave:显示灰度矩阵显示和全黑图像的功能
- javascript - Discord Giveaway 机器人始终将机器人作为赢家
- python - 使用 BeautifulSoup Python 提取 id 的内容