Guest User

Untitled

a guest
Nov 18th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. using Microsoft.CodeAnalysis;
  2. using Microsoft.CodeAnalysis.CSharp.Syntax;
  3. using Microsoft.CodeAnalysis.Text;
  4. using System.Linq;
  5. using System;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApp2
  9. {
  10. class Program
  11. {
  12. class Structure { }
  13. static async Task Main(string[] args)
  14. {
  15. //Nuget: Microsoft.CodeAnalysis
  16. var workspace = new AdhocWorkspace();
  17. var newProjectId = workspace.AddProject("NewProject", LanguageNames.CSharp).Id;
  18. var sourceText = SourceText.From("" +
  19. @"
  20. class Program
  21. {
  22. class Structure { }
  23. static void Main(string[] args)
  24. {
  25.  
  26. var structure = new Structure();
  27. structure
  28. structure.A = 0;
  29. }
  30. }
  31. ");
  32. var newDocument = workspace.AddDocument(newProjectId, "NewFile.cs", sourceText);
  33. var syntaxRoot = await newDocument.GetSyntaxRootAsync();
  34. var syntaxTree = syntaxRoot.SyntaxTree;
  35. var compilation = await newDocument.Project.GetCompilationAsync();
  36. var semanticModel = compilation.GetSemanticModel(syntaxTree);
  37. var varDecl = syntaxRoot.DescendantNodes().OfType<VariableDeclarationSyntax>().Skip(1).First();
  38. var id = varDecl.DescendantNodes().OfType<IdentifierNameSyntax>().First();
  39. var ti = semanticModel.GetSymbolInfo(id);
  40. }
  41. }
  42. }
Add Comment
Please, Sign In to add comment