Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create MSBuild custom task to modify C# code *before* compile

I want to create a custom MSBuild task that changes my .cs files before they are compiled by csc.exe (but, of course, that doesn't modify them in place - I don't want actual source files touched). I am aware of PostSharp and other AOP frameworks for .NET and they are not an option for this particular project, plus I'd like to learn how to do this.

What precisely do I have to do to get this to work?

Thanks Richard

like image 708
ZeroBugBounce Avatar asked Sep 30 '08 15:09

ZeroBugBounce


1 Answers

Given your restrictions I think you can do the following:

  1. Create custom task that accepts the list of cs files to adapt prior to compilation
  2. The custom task adapts the list of files received and creates them on disk
  3. The custom task sets the list of changed files on the output parameter
  4. The output of the task would replace the original cs files list
  5. The compilation is done against the changed files.

The step 4 ensures that the files that are eventually compiled are the ones that were changed by your custom task.

You will heavily rely on the ITaskItem interface for the job.

like image 85
Jorge Ferreira Avatar answered Oct 24 '22 06:10

Jorge Ferreira