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

余生长醉 提交于 2019-12-06 01:06:12

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/153602/create-msbuild-custom-task-to-modify-c-sharp-code-before-compile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!