Compile-time source code modification using Roslyn

.net, c#, msbuild, roslyn

Solution

This is definitely a scenario that we are thinking of. Today there are a couple of problems that make it a bit difficult:

- You can't use the Workspace APIs to load a project/solution as you are already inside of msbuild.

- To use the regular compiler APIs, you need to construct a compilation yourself which can be a bunch of work.

In the future, we'd like to provide a "Create a workspace from a csc/vbc command line string", which would make this a lot easier.

Take a look at Hooking into the compiler (csc.exe or vbc.exe) itself and Problem with using Roslyn in a MS Build Task for some previous discussion on this.

Problem

Is it possible to modify source code before compilation using Roslyn within MSBuild task on CI server? I've succeeded to do what I want in VS but I wonder if it is possible outside VS. Currently I'm looking at Workspace APIs and Compiler APIs and they seem to be the right tool to achieve that, but I'm still not sure is it possible at all? In particular I'm concerned about returning changes that I've done to MSBuild back to allow it to continue its job.

Original source