Skip to content

Instantly share code, notes, and snippets.

@JoshVarty
Created July 6, 2014 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshVarty/37b27f698fd8905cfda2 to your computer and use it in GitHub Desktop.
Save JoshVarty/37b27f698fd8905cfda2 to your computer and use it in GitHub Desktop.
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
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