简体   繁体   中英

Pre-build action

As part of a build process, I need to replace some of the code in ac# project at pre-build time. I hoped there is some hook available to specify in .csproj file. I am using Rider. This article appears to be about what I need; https://docs.microsoft.com/en-us/visualstudio/ide/reference/pre-build-event-post-build-event-command-line-dialog-box?view=vs-2019 , but I am not sure how to use it in .csproj .

How do you specify pre-build actions in c#?

You could define a BeforeBuild target in your csproj to run a command. Say, for example, I had a PS script I wanted to run:

<Target Name="BeforeBuild">                                                                    
   <Exec Command="$(PowerShellExe) -NonInteractive -executionpolicy Unrestricted 
             -command &quot;&amp; invoke-command -scriptblock { 
                      &amp;&apos;$(ScriptLocation)&apos; 
                      &apos;$(LogFileLocation)&apos;  
                      &apos;$(MSDeployComputerName)&apos;}
                      &quot;"/>    
</Target>

See docs .

Inside the .csproj you can add this:

<PropertyGroup>
    <PreBuildEvent>start %25comspec%25</PreBuildEvent>
</PropertyGroup>

This only starts a cmd Window in that case but you can replace it. Also you can place it in the property group you want to use. See docs

Also I want to note the preprocessor directives specifically #if . When you want to replace DEBUG Code, that only should be present in DEBUG Environment and it's a static replacement, this would be easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM