[英]StartupObject has different behavior when running from command line than when modifying CSPROJ file
I just stumbled across the StartupObject
parameter for MSBuild, which allows for explicit specification of a projects entry point.我刚刚偶然发现了 MSBuild 的
StartupObject
参数,它允许明确指定项目入口点。 Based on this question it looks like there are two ways to specify this parameters:基于这个问题,看起来有两种方法可以指定这个参数:
dotnet
CLI.dotnet
CLI 指定为 MSBuild 属性。 I'm interested in the second option, so I made a simple console application with two files, First.cs
and Second.cs
:我对第二个选项感兴趣,所以我制作了一个简单的控制台应用程序,其中包含两个文件
First.cs
和Second.cs
:
First.cs
using System;
internal class First
{
static void Main(string[] args)
{
Console.WriteLine("First");
}
}
Second
using System;
internal class Second
{
static void Main(string[] args)
{
Console.WriteLine("Second");
}
}
What I found out is that specifying StartupObject
via the dotnet
CLI will call the correct startup object only if running from a clean slate:我发现通过
dotnet
CLI 指定StartupObject
将调用正确的启动对象,前提是从干净的状态运行:
PS> dotnet run --project=test_multiple_entry -p:StartupObject=First
First
PS> dotnet run --project=test_multiple_entry -p:StartupObject=Second
First
PS> dotnet clean test_multiple_entry
...
PS> dotnet run --project=test_multiple_entry -p:StartupObject=Second
Second
On the other hand, if I actually modify the entry point from the CSPROJ file, no dotnet clean
is necessary, and it will automatically go find the correct entry point.另一方面,如果我真的从 CSPROJ 文件修改入口点,则不需要
dotnet clean
,它会自动找到正确的入口点。
I'm curious as to whether this behavior is intentional?我很好奇这种行为是否是故意的? Why does
dotnet run
not use the correct entry point unless preceded by a dotnet clean
?为什么
dotnet run
不使用正确的入口点,除非前面有dotnet clean
? Why does modifying the CSPROJ file directly simply work?为什么直接修改 CSPROJ 文件很简单?
StartupObject is a build property, not a runtime property. StartupObject 是构建属性,而不是运行时属性。 When
dotnet
run sees that a build is required (such as a source file (including the csproj) is newer than the build output, or there's no build output (after dotnet clean
)) then it cascades to dotnet build
first, and passes through all the -p arguments to the build.当
dotnet
run 发现需要构建(例如源文件(包括 csproj)比构建输出新,或者没有构建输出(在dotnet clean
之后))时,它首先级联到dotnet build
,并通过所有构建的 -p 参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.