'dotnet build' specify main method

后端 未结 2 1762
臣服心动
臣服心动 2020-12-16 12:09

I am using dotnet to build a .NET Core C# project from the command line. The project has multiple classes with a main method. Thus I get the error:

相关标签:
2条回答
  • 2020-12-16 12:35

    You can edit your csproj to define which class to use (inside a PropertyGroup):

    <StartupObject>foo.Program2</StartupObject>
    

    or specify this MSBuild property on the command line via:

    $ dotnet build foo.csproj /p:StartupObject=foo.Program2
    
    0 讨论(0)
  • 2020-12-16 12:55

    Just to add why calling dotnet with /main fails with that error, note that it says "Compile with /main"; /main is a parameter for the compiler (csc.exe), not dotnet build. dotnet build will invoke MSBuild.exe which, in turn, will invoke csc.exe, but you'll need to tell dotnet build what the startup class is so it can tell csc.exe. This is what the accepted answer does.

    Alternatively, if you were calling csc.exe directly, you could pass /main to it like so...

    csc.exe Program.cs Test.cs /main:TestNamespace.Test
    
    0 讨论(0)
提交回复
热议问题