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.
我想创建一个自定义的MSBuild任务,在csc.exe编译之前更改我的.cs文件(当然,这不会修改它们 - 我不希望触及实际的源文件)。我知道PostSharp和.NET的其他AOP框架,它们不是这个特定项目的选项,而且我想学习如何做到这一点。
What precisely do I have to do to get this to work?
我究竟需要做些什么才能让它发挥作用?
Thanks Richard
1 个解决方案
#1
5
Given your restrictions I think you can do the following:
鉴于您的限制,我认为您可以执行以下操作:
- Create custom task that accepts the list of cs files to adapt prior to compilation
- The custom task adapts the list of files received and creates them on disk
- The custom task sets the list of changed files on the output parameter
- The output of the task would replace the original cs files list
- The compilation is done against the changed files.
创建自定义任务,在编译之前接受要调整的cs文件列表
自定义任务会调整接收的文件列表并在磁盘上创建它们
自定义任务在输出参数上设置已更改文件的列表
任务的输出将替换原始的cs文件列表
编译是针对更改的文件完成的。
The step 4 ensures that the files that are eventually compiled are the ones that were changed by your custom task.
步骤4确保最终编译的文件是您的自定义任务更改的文件。
You will heavily rely on the ITaskItem interface for the job.
您将非常依赖ITaskItem接口来完成工作。
#1
5
Given your restrictions I think you can do the following:
鉴于您的限制,我认为您可以执行以下操作:
- Create custom task that accepts the list of cs files to adapt prior to compilation
- The custom task adapts the list of files received and creates them on disk
- The custom task sets the list of changed files on the output parameter
- The output of the task would replace the original cs files list
- The compilation is done against the changed files.
创建自定义任务,在编译之前接受要调整的cs文件列表
自定义任务会调整接收的文件列表并在磁盘上创建它们
自定义任务在输出参数上设置已更改文件的列表
任务的输出将替换原始的cs文件列表
编译是针对更改的文件完成的。
The step 4 ensures that the files that are eventually compiled are the ones that were changed by your custom task.
步骤4确保最终编译的文件是您的自定义任务更改的文件。
You will heavily rely on the ITaskItem interface for the job.
您将非常依赖ITaskItem接口来完成工作。