dotnet build指定main方法

时间:2017-04-12 09:14:16

标签: c# msbuild .net-core

我正在使用dotnet从命令行构建.NET Core 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.

传递/main开关会导致错误:

$ 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命令?

2 个答案:

答案 0 :(得分:29)

您可以编辑csproj以定义要使用的类(在PropertyGroup内):

<StartupObject>foo.Program2</StartupObject>

或通过以下命令行指定此MSBuild属性:

$ dotnet build foo.csproj /p:StartupObject=foo.Program2

答案 1 :(得分:1)

仅添加为什么用dotnet调用/main失败并显示该错误的原因,请注意它说“ 使用”; /mainparameter for the compiler/main),而不是dotnet buildcsc.exe将调用MSBuild.exe,后者又将调用dotnet build,但是您需要告诉csc.exe启动类是什么,以便它可以告诉{{1} }。这就是公认的答案。

或者,如果您直接呼叫dotnet build,则可以像这样将csc.exe传递给它...

csc.exe