Suppose I have a console app containing two CS files:
Program1.cs
using System;
namespace MyConsoleApp
{
class Program1
{
pubic static void Main()
{
Console.WriteLine("From Program1");
}
}
}
Program2.cs
using System;
namespace MyConsoleApp
{
class Program2
{
pubic static void Main()
{
Console.WriteLine("From Program2");
}
}
}
If I try to run this app using the dotnet CLI (dotnet run) I'm going to get an error:
error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point
As far as I know, /main is only a flag to csc and not to msbuild. In order to pinpoint the startup type with msbuild one must specify it in the CSPROJ file as MainEntryPoint or StartupObject.
In my case, I'd like to be able to specify this on the fly through the dotnet CLI. Unfortunately the following still throws the exact same error:
dotnet run msbuild /main:MyConsoleApp.Program1
How do I pass csc's /main switch to msbuild through the dotnet CLI?
Edit
This is not a duplicate because the /p switch doesn't help:
$ dotnet build /p:StartupObject=MyConsoleApp.Program2
Microsoft (R) Build Engine version 16.11.3+5d7fe36cf for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
Restored C:\Users\MyConsoleApp\MyConsoleApp.csproj (in 86 ms).
MyConsoleApp -> C:\Users\MyConsoleApp\bin\Debug\netcoreapp3.1\MyConsoleApp.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:01.76
$ dotnet run
From Program1