Skip to content

Instantly share code, notes, and snippets.

@sudipto80
Created October 10, 2014 08:39
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/824c84bac10a9e1e3780 to your computer and use it in GitHub Desktop.
Save sudipto80/824c84bac10a9e1e3780 to your computer and use it in GitHub Desktop.
Meta Programming using Roslyn (Finding LoC for all the methods)
SyntaxTree tree = SyntaxTree.ParseText(@"int fun(int x){ int y = 10; x++;
if(x>20) x -= y; 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();
int lvc = methods[0].Body.Statements.Count(x => x.Kind == SyntaxKind.LocalDeclarationStatement);
methods.Select(z => new { MethodName = z.Identifier.ValueText, LoC = z.Body.Statements.Count })
.OrderByDescending(x => x.LoC)
.ToList()
.ForEach(x => Console.WriteLine(x.MethodName + " " + x.LoC));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment