从命令行运行时,启动UpObject与修改CSPROJ文件时具有不同的行为
我只是偶然发现了MSBUILD的startupobject
参数,该参数允许明确规范项目入口点。基于这个问题看来有两种方法可以指定此参数:
- 在给定项目的CSPROJ文件,
- 使用
dotnet
cli指定为MSBUILD属性。
我对第二个选项感兴趣,因此我用两个文件制作了一个简单的控制台应用程序,first.cs
and second.cs
:
first.cs 代码>
using System;
internal class First
{
static void Main(string[] args)
{
Console.WriteLine("First");
}
}
第二个
using System;
internal class Second
{
static void Main(string[] args)
{
Console.WriteLine("Second");
}
}
我发现的是,通过dotnet
CLI指定startupobject
CLI将调用正确的启动对象唯一如果从干净的板岩运行:
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
另一方面,如果我实际上修改了CSPROJ文件的入口点,则无需dotnet Clean
,它将自动找到正确的入口点。
我很好奇这种行为是否是故意的?为什么dotnet运行
除非dotnet Clean
在之前不使用正确的入口点?为什么修改CSPROJ文件直接工作?
I just stumbled across the StartupObject
parameter for MSBuild, which allows for explicit specification of a projects entry point. Based on this question it looks like there are two ways to specify this parameters:
- In the CSPROJ file for the given project
- Specifying as an MSBuild property using
dotnet
CLI.
I'm interested in the second option, so I made a simple console application with two files, First.cs
and 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:
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.
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
? Why does modifying the CSPROJ file directly simply work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
startupobject是构建属性,而不是运行时属性。当
dotnet
运行时,会看到需要构建(例如源文件(包括csproj),比构建输出更新,或者没有构建输出(dotnet clean
之后))然后它首先级联到dotnet build
,然后将所有-P参数传递给构建。StartupObject is a build property, not a runtime property. 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 (afterdotnet clean
)) then it cascades todotnet build
first, and passes through all the -p arguments to the build.