Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
var tree = CSharpSyntaxTree.ParseText(@"
public class MyClass
public void MyMethod()
var syntaxRoot = tree.GetRoot();
var MyClass = syntaxRoot.DescendantNodes().OfType<ClassDeclarationSyntax>().First();
var MyMethod = syntaxRoot.DescendantNodes().OfType<MethodDeclarationSyntax>().First();
Copy link

nattomi commented Apr 20, 2021

I had to add using System.Linq to bring .OfType() into scope

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment