c# – Reload the project after the pre-build event

Question:

In a .NET project, a pre-build event is configured that generates several source files, it is necessary that these files be included in the project before the build begins. I read here that you can specify a folder in the .csproj file, the contents of which will be included in the project (and it really works), for example <Content Include="AutoCodegen\**" /> includes all files from the AutoCodegen folder in the project, but this happens only after restarting the project (for example restarting the studio). Is there a way to reload the project after the pre-buil event or is there some other way to achieve my goal? Thank you.

Answer:

Instead of pre-build event, you need to use your target in the project file. Internally, you can use the Exec task.

After completing this task, you can replace the list of input files for compilation:

<Target Name="ВашТаргет" BeforeTargets="BeforeCompile">
  <Exec ... />

  <ItemGroup>
    <Compile Remove="AutoCodegen\**" />
    <Compile Include="AutoCodegen\**" />
  </ItemGroup>
</Target>

Such files added dynamically will not be visible in the list of project files – but will be counted during compilation.

Limitation. This method is incompatible with R # – it flatly refuses to see files dynamically added to the project.

Scroll to Top