Skip to content

Instantly share code, notes, and snippets.

@jmarolf
Created April 4, 2015 22:46
Show Gist options
  • Save jmarolf/4b55afdd9a4c5269580c to your computer and use it in GitHub Desktop.
Save jmarolf/4b55afdd9a4c5269580c to your computer and use it in GitHub Desktop.
Get parse errors from string.
using System;
using System.Collections;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
class Program
{
static void Main(string[] args)
{
var tree = CSharpSyntaxTree.ParseText(SourceText.From("class a{}"));
var compilation = CSharpCompilation.Create("test", new[] { tree });
compilation = compilation.AddReferences(
MetadataReference.CreateFromAssembly(typeof(object).Assembly),
MetadataReference.CreateFromAssembly(typeof(IEnumerable).Assembly));
var diagnostics = compilation.GetParseDiagnostics();
foreach (var diagnostic in diagnostics)
{
Console.WriteLine(diagnostic);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment