Skip to content

Instantly share code, notes, and snippets.

@barik
Created April 10, 2015 14:36
Show Gist options
  • Save barik/5570b3dca9089dad56ae to your computer and use it in GitHub Desktop.
Save barik/5570b3dca9089dad56ae to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace RoslynDemo
{
class Program
{
static void Main(string[] args)
{
string sourceText = @"
using System;
using System.Collections.Generic;
using System.Text;
namespace HelloWorld {
class Program
{
static void Main(string[] args)
{
int x = 5
}
}
}
";
var tree = CSharpSyntaxTree.ParseText(sourceText);
var root = (CompilationUnitSyntax)tree.GetRoot();
// https://github.com/dotnet/roslyn/issues/932
var compilation = CSharpCompilation.Create("helloWorld")
.AddReferences(MetadataReference.CreateFromAssembly(typeof (object).Assembly))
.AddSyntaxTrees(tree);
var diagnostic = compilation.GetDiagnostics();
foreach (var d in diagnostic)
{
Console.WriteLine(d.Descriptor.Id + ":" + d.Descriptor.Category + " - " + d.GetMessage() + " [" + d.Severity + "]");
Console.WriteLine("\t" + d.Location);
}
var model = compilation.GetSemanticModel(tree);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment