【问题标题】:'dotnet build' specify main method'dotnet build' 指定主要方法
【发布时间】:2017-09-07 23:33:23
【问题描述】:

我正在使用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 命令?

【问题讨论】:

    标签: c# msbuild .net-core


    【解决方案1】:

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

    <StartupObject>foo.Program2</StartupObject>
    

    或通过以下方式在命令行上指定此 MSBuild 属性:

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

    在哪里

    namespace foo
    {
      class Program2{ public static void Main() {} }
    }
    

    【讨论】:

    • 我在过去的一个小时里尝试了一些事情,直到找到你的答案。非常感谢您抽出宝贵时间记录此内容!
    • 这里的游戏迟到了,但在 Web 应用程序中有意义吗?在 VS 2017 中,启动对象设置为 Project。不要认为这是标签的有效值。此外,不幸的是,我没有测试项目(现有代码库),因此与应用程序入口点和测试项目入口点相关的答案不适用。请指教。
    • @EoRaptor013 这个问题是关于具有公共静态Main() 方法的多个类,编译器需要在其中一个方法中做出决定。这与包含多个项目的 sln 文件无关。
    • 什么是foo,什么是Program2?是命名空间和文件名吗?
    • @Morasiu 它想要包含Main 方法的类的全名(即Namespace.ClassName):这里foo 是NameSpace,Program2 是类的名称(不必在名为Program2.cs 的文件中)
    【解决方案2】:

    只是添加为什么用/main 调用dotnet 会失败并出现该错误,请注意它说“Compile with /main”; /mainparameter for the compiler (csc.exe),而不是 dotnet builddotnet build 将调用MSBuild.exe,而csc.exe 将调用csc.exe,但您需要告诉dotnet build 启动类是什么,以便它可以告诉csc.exe。这就是接受的答案的作用。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-02
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 2020-06-14
      • 2018-02-27
      • 1970-01-01
      • 2015-04-24
      相关资源
      最近更新 更多