Skip to content

Instantly share code, notes, and snippets.

Embed
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();
Console.WriteLine(MyClass.Identifier.ToString());
Console.WriteLine(MyMethod.Identifier.ToString());
@nattomi

This comment has been minimized.

Copy link

@nattomi 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