为什么 .NET Core CLI(“dotnet”)除了“exe”文件之外还会生成“dll”文件?

Tro*_*yvs 5 dll console build .net-core

我正在尝试“dotnet”命令行工具。例如,我可以运行:

dotnet new console
Run Code Online (Sandbox Code Playgroud)

这会创建一个新项目,然后

dotnet build
Run Code Online (Sandbox Code Playgroud)

它将生成bin/Debug/netcoreapp3.1/XXX.exebin/Debug/netcoreapp3.1/XXX.dll

为什么会生成DLL文件呢?在其他情况下也能用吗?

met*_*ube 4

当你使用命令时

dotnet new console
Run Code Online (Sandbox Code Playgroud)

生成的项目文件如下所示

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

</Project>
Run Code Online (Sandbox Code Playgroud)

线

<OutputType>Exe</OutputType>
Run Code Online (Sandbox Code Playgroud)

指定将生成 .exe。该可执行文件只是运行存储在 DLL 文件中的应用程序(DLL 文件可以直接通过 执行dotnet full_generated_dll_path)。我猜 .exe 输出在服务/守护进程场景中很方便。

项目通常具有一些依赖项(托管和非托管),因此输出包含大量 DLL 文件。看起来没有理由合并 EXE 文件和 DLL 文件,因为您仍然有许多其他 DLL 文件(并且应用程序逻辑可能也应该与执行应用程序的方式分开)。

从分发的角度来看,包含执行应用程序所需的所有内容的单个文件似乎很方便,因此此功能被添加到.NET Core 3 中。