dotnet build指定main方法

sak*_*kra 15 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.
Run Code Online (Sandbox Code Playgroud)

传递/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
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*ich 29

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

<StartupObject>foo.Program2</StartupObject>
Run Code Online (Sandbox Code Playgroud)

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

$ dotnet build foo.csproj /p:StartupObject=foo.Program2
Run Code Online (Sandbox Code Playgroud)

  • 我花了一个小时尝试了一些事情,直到找到你的答案.非常感谢您花时间记录这个! (2认同)
  • @Morasiu,它需要包含一个Main方法的类的全名(即Namespace.ClassName):这里foo是NameSpace,Program2是类的名称(不必在名为“ Program2.cs”的文件) (2认同)