Skip to content

Instantly share code, notes, and snippets.

@jmarolf
Created March 27, 2015 22:52
Show Gist options
  • Save jmarolf/2325f55a12f2e1c48aa7 to your computer and use it in GitHub Desktop.
Save jmarolf/2325f55a12f2e1c48aa7 to your computer and use it in GitHub Desktop.
Adding reference and modifying document
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
var workspace = MSBuildWorkspace.Create();
RunAsync(workspace, args).GetAwaiter().GetResult(); ;
}
private static async Task RunAsync(MSBuildWorkspace workspace, string[] args)
{
var solution = await workspace.OpenSolutionAsync(args[0]);
var project = solution.Projects.Single();
var projectWithReference = project.AddMetadataReference(MetadataReference.CreateFromFile(@"path\to\reference.dll"));
var docuemnt = projectWithReference.Documents.Single();
var reWritenDocument = Rewrite(docuemnt);
var compilation = await reWritenDocument.Project.GetCompilationAsync();
var diagnostics = compilation.GetDiagnostics();
foreach (var diagnostic in diagnostics)
{
Console.WriteLine(diagnostic);
}
}
private static Document Rewrite(Document document)
{
// change document to call the static method
throw new NotImplementedException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment