Created
April 10, 2015 14:36
-
-
Save barik/5570b3dca9089dad56ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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