Hook into the Roslyn build process

1.3k views Asked by At

Is it possible to hook into the Roslyn build process during a Visual Studio/TFS build, and if yes, is it possible to get a hold of the Microsoft.CodeAnalysis.Solution/Microsoft.CodeAnalysis.Project instance being used by Roslyn during compilation?

1

There are 1 answers

0
Alex Endris On

The way I see it, Jon is pretty much right in his comment. What I would suggest is to create an MSBuild task, which is your wanted hook into the build process.

Create an MSBuild project file (you've probably seen them already, it's those files that have the .targets extension). It looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <UsingTask TaskName="MyNamespace.MyReferenceValidationTask" AssemblyFile="MyPath\MyNamespace.dll"/>
    <Target 
        BeforeTargets="BeforeCompile"
        Name="ValidationTarget">
        <MyNamespace.MyReferenceValidationTask
            SolutionRoot="$(SolutionDir)" />
    </Target>
</Project>

The attribute in the "MyNamespace.MyReferenceValidationTask" tag "SolutionRoot" is your property in your task. All the macros that are available in Visual Studio are also available here. (see this post here: https://stackoverflow.com/a/1453023/978594)

What you do inside of the task is entirely up to you. You can, for example, load your solution file with Roslyn and thus have all the projects and their references and do your desired validation.