Created
October 10, 2014 08:40
-
-
Save sudipto80/3c286e7167e99dc6a6bc to your computer and use it in GitHub Desktop.
Meta Programming using Roslyn (Finding all Local Variables for all the methods)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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