C# ';dotnet构建&x27;指定主要方法

C# ';dotnet构建&x27;指定主要方法,c#,msbuild,.net-core,C#,Msbuild,.net Core,我正在使用dotnet从命令行构建一个.NET核心C#项目。该项目有多个类,使用main方法。因此我得到了一个错误: $ dotnet build Microsoft (R) Build Engine version 15.1.548.43366 Copyright (C) Microsoft Corporation. All rights reserved. Test.cs(18,28): error CS0017: Program has more than one entry point

我正在使用
dotnet
从命令行构建一个.NET核心C#项目。该项目有多个类,使用
main
方法。因此我得到了一个错误:

$ dotnet build
Microsoft (R) Build Engine version 15.1.548.43366
Copyright (C) Microsoft Corporation. All rights reserved.

Test.cs(18,28): error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.

Build FAILED.
$ dotnet build /main:Test
Microsoft (R) Build Engine version 15.1.548.43366
Copyright (C) Microsoft Corporation. All rights reserved.

MSBUILD : error MSB1001: Unknown switch.
Switch: /main:Test
传递
/main
开关会导致错误:

$ dotnet build
Microsoft (R) Build Engine version 15.1.548.43366
Copyright (C) Microsoft Corporation. All rights reserved.

Test.cs(18,28): error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.

Build FAILED.
$ dotnet build /main:Test
Microsoft (R) Build Engine version 15.1.548.43366
Copyright (C) Microsoft Corporation. All rights reserved.

MSBUILD : error MSB1001: Unknown switch.
Switch: /main:Test

如何将
/main
开关传递到
dotnet
命令?

您可以编辑csproj以定义要使用的类(在
属性组中)

在哪里


要添加使用
/main
调用
dotnet
失败的原因,请注意,它会显示“使用
/main
编译
/main
是(
csc.exe
),而不是
dotnet build
将调用它,然后调用
csc.exe
,但您需要告诉
dotnet build
启动类是什么,这样它才能告诉
csc.exe
。这就是公认的答案

或者,如果您直接调用
csc.exe
,您可以像这样将
/main
传递给它

csc.exe Program.cs Test.cs /main:TestNamespace.Test

我花了一个小时试了几件事,直到找到你的答案。非常感谢您花时间来记录这篇文章!这里的游戏很晚,但是在Web应用程序中有意义吗?在VS 2017中,启动对象设置为Project。不要认为这是标记的有效值。此外,不幸的是,我没有测试项目(现有代码库),因此与应用程序入口点和测试项目入口点相关的答案不适用。请给出建议。@EoRaptor013这个问题是关于多个具有at public static
Main()
方法的类,编译器需要在其中的一个类之间做出决定。这与包含多个项目的sln文件无关。什么是
foo
,什么是
Program2
?它是名称空间和文件名吗?@Morasiu它需要包含
Main
方法的类(即
namespace.ClassName
)的全名:这里
foo
将是名称空间,
Program2
将是类的名称(不需要在名为
Program2.cs
的文件中)
csc.exe Program.cs Test.cs /main:TestNamespace.Test