简体   繁体   English

StartupObject 从命令行运行时的行为与修改 CSPROJ 文件时的行为不同

[英]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:基于这个问题,看起来有两种方法可以指定这个参数:

  • In the CSPROJ file for the given project在给定项目的 CSPROJ 文件中
  • Specifying as an MSBuild property using 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.csSecond.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.

相关问题 从命令行执行与双击时从 WPF 应用程序运行 psexec 的不同行为与双击 - Different behavior with running psexec from WPF app when executing from command line vs. double-click 使用命令行脚本或从后台工作程序运行它时的不同行为 - Different behaviour when using a command line script or running it from a backgroundworker 从PowerShell脚本调用时,MSBuild使用.sln;而从命令行调用时,则使用.csproj - MSBuild uses .sln when called from PowerShell script but .csproj when called from command line 在 Powershell 中运行时与在 Visual Studio 中运行时的 HttpClient 并发行为不同 - HttpClient concurrent behavior different when running in Powershell than in Visual Studio 通过命令行运行NUnit测试时使用runsettings文件 - Using runsettings file when running NUnit tests via command line 从命令行运行时,mstest v2会忽略ExpectedException - ExpectedException ignored by mstest v2 when running from the command line 双击时无法访问 csproj 文件 - Cannot access csproj file when double clicking on it 测试.csproj(C#)时出现运行Rescharper检查代码的问题-未获取dotsettings文件 - Issue running rescharper inspect code - dotsettings file not picked up when testing .csproj (c#) 从 Visual Studio 和直接从 exe 运行应用程序时,激活窗口行为是不同的 - Activating a window behavior is different when running the app from Visual Studio and directly from the exe .csproj 文件何时更新? - When does the .csproj file get updated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM