在 C# 项目中构建两个程序 [英] Building two programs in a C# project

查看:39
本文介绍了在 C# 项目中构建两个程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

短版

在由 dotnet new console 创建的 C# 项目中,所有 *.cs 文件,甚至子目录中的文件,都被编译成一个程序.如果有两个具有 Main 函数的类,则构建失败.我如何告诉它构建两个程序?

In a C# project created by dotnet new console, all of the *.cs files, even the ones in subdirectories, are compiled into a single program. If there are two classes with Main functions, it fails to build. How do I tell it to build two programs?

更多详情

作为 C# 和所有相关工具的新手,我遵循了教程,并学习了如何通过运行 dotnet new console 创建一个最小的项目,加入一些 *.cs 文件,然后使用 dotnet run 运行生成的程序.我什至用 dotnet publish 做了一个可用的发布".所有这一切都由这个 *.csproj 文件协调:

As a newcomer to C# and all of the related tools, I followed the tutorials, and learned how to create a minimal project by running dotnet new console, throw in some *.cs files, and run the resulting program with dotnet run. I even made a usable "release" with dotnet publish. All of this is coordinated by this *.csproj file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2"/>
  </ItemGroup>
</Project>

如果 A.csB.cs 都声明 Main,则构建失败并显示 错误 CS0017: Program has more than定义了一个入口点.使用/main 编译以指定包含入口点的类型..所以我知道有一个 /main 选项,但我不知道把它放在哪里.dotnet run/main A.cs 不是.

If A.cs and B.cs both declare Main, the build fails with error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.. So I have a clue that there is a /main option, but I don't know where to put it. dotnet run /main A.cs is not it.

目标是让一些看起来像 dotnet run/main A.cs 的东西,但实际上是有效的;并让 dotnet publish 同时创建 A.exeB.exe(或与目标平台最接近的等价物).

The goal is to have something that looks like dotnet run /main A.cs, but actually works; and to have dotnet publish create both A.exe and B.exe (or the closest equivalent for the target platform).

我希望我将不得不进行一些重要的 *.csproj 编辑,但我对那个文件的了解是 dotnet new 创建它,并且 dotnet add package 放在 PackageReference 中.实际的构建规则隐藏在 Sdk 中,我不知道如何控制它的作用.

I expect that I will have to do some non-trivial *.csproj editing, but all I know about that file is that dotnet new created it, and dotnet add package put in the PackageReference. The actual build rules are hidden away in the Sdk and I don't know how to control what it does.

相关问题

这个问题看起来和我的一样,但接受的答案只构建了其中一个程序.

This question looks the same as mine, but the accepted answer only builds one of the programs.

推荐答案

您必须有两个单独的项目才能生成两个单独的 EXE 文件.共享代码通常会出现在两者都依赖的第三个库"项目中.虽然你可以让一个应用项目依赖于另一个,但这会有点奇怪.

You have to have two separate projects to produce two separate EXE files. Shared code will usually go in a third "Library" project that both depend on. Though you can make one application project depend on the other, but this would be a bit odd.

这篇关于在 C# 项目中构建两个程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆