Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.CodeAnalysis;
- using Microsoft.CodeAnalysis.CSharp.Syntax;
- using Microsoft.CodeAnalysis.Text;
- using System.Linq;
- using System;
- using System.Threading.Tasks;
- namespace ConsoleApp2
- {
- class Program
- {
- class Structure { }
- static async Task Main(string[] args)
- {
- //Nuget: Microsoft.CodeAnalysis
- var workspace = new AdhocWorkspace();
- var newProjectId = workspace.AddProject("NewProject", LanguageNames.CSharp).Id;
- var sourceText = SourceText.From("" +
- @"
- class Program
- {
- class Structure { }
- static void Main(string[] args)
- {
- var structure = new Structure();
- structure
- structure.A = 0;
- }
- }
- ");
- var newDocument = workspace.AddDocument(newProjectId, "NewFile.cs", sourceText);
- var syntaxRoot = await newDocument.GetSyntaxRootAsync();
- var syntaxTree = syntaxRoot.SyntaxTree;
- var compilation = await newDocument.Project.GetCompilationAsync();
- var semanticModel = compilation.GetSemanticModel(syntaxTree);
- var varDecl = syntaxRoot.DescendantNodes().OfType<VariableDeclarationSyntax>().Skip(1).First();
- var id = varDecl.DescendantNodes().OfType<IdentifierNameSyntax>().First();
- var ti = semanticModel.GetSymbolInfo(id);
- }
- }
- }
Add Comment
Please, Sign In to add comment