Skip to content

Instantly share code, notes, and snippets.

@sudipto80
Created October 10, 2014 08:40
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 sudipto80/3c286e7167e99dc6a6bc to your computer and use it in GitHub Desktop.
Save sudipto80/3c286e7167e99dc6a6bc to your computer and use it in GitHub Desktop.
Meta Programming using Roslyn (Finding all Local Variables for all the methods)
SyntaxTree tree = SyntaxTree.ParseText(@"int fun(int x){ int y = 0; x++; return x+1;}
double funny(double x){ int s = 3; double t = 4; return x + s * t/2.13;}");
List<MethodDeclarationSyntax> methods = tree.GetRoot()
.DescendantNodes()
.Where(d => d.Kind == SyntaxKind.MethodDeclaration)
.Cast<MethodDeclarationSyntax>()
.ToList();
methods.Select(z => new { MethodName = z.Identifier.ValueText,
NBLocal = z.Body.Statements.Count(x => x.Kind == SyntaxKind.LocalDeclarationStatement) })
.OrderByDescending(x => x.NBLocal)
.ToList()
.ForEach(x => Console.WriteLine(x.MethodName + " " + x.NBLocal));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment