Skip to content

Instantly share code, notes, and snippets.

@JoshVarty
Created July 6, 2014 21:17
Show Gist options
  • Save JoshVarty/e377af43df8775559c03 to your computer and use it in GitHub Desktop.
Save JoshVarty/e377af43df8775559c03 to your computer and use it in GitHub Desktop.
var tree = CSharpSyntaxTree.ParseText(@"
public class MyClass
{
public void MyMethod()
{
}
public void MyMethod(int n)
{
}
}");
var syntaxRoot = tree.GetRoot();
var MyMethod = syntaxRoot.DescendantNodes().OfType<MethodDeclarationSyntax>()
.Where(n => n.ParameterList.Parameters.Any()).First();
var containingType = MyMethod.Ancestors().OfType<ClassDeclarationSyntax>().First();
Console.WriteLine(containingType.Identifier.ToString());
Console.WriteLine(MyMethod.ToString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment