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

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

问题描述

简短版本

dotnet新控制台创建的C#项目中,所有 *。cs 文件,甚至子目录中的文件,都被编译到一个程序中。如果有两个具有 Main 函数的类,它将无法生成。我如何告诉它构建2个程序?

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 2 classes with Main functions, it fails to build. How do I tell it to build 2 programs?

更多详细信息

我是C#和所有相关工具的新手,我按照这些教程学习了如何通过运行 dotnet新控制台创建最小项目,并添加了一些 *。cs 文件,然后使用 dotnet run 运行生成的程序。我什至通过 dotnet发布创建了一个可用的发布。所有这些都由以下 *。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.cs B.cs 都声明 Main ,构建失败,并出现错误CS0017:程序有多个入口点定义。用/ main编译以指定包含入口点的类型。。所以我有一个提示,有一个 / main 选项,但是我不知道该放在哪里。 dotnet运行/ 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运行/ main A.cs ,但实际上有效;并发布 dotnet 创建 A.exe B.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是新的创建了该文件,然后 dotnet添加了软件包放在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 EXEs. 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#项目中构建2个程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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