Created
October 10, 2014 08:38
-
-
Save sudipto80/a008f7f992d7a8f01d71 to your computer and use it in GitHub Desktop.
Meta Programming using Roslyn (Finding all methods that don't use all the parameters)
This file contains 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 z){ int y = 0; x++; return x+1;} | |
double funny(double x){ return x/2.13;}"); | |
List<MethodDeclarationSyntax> methods = tree.GetRoot() | |
.DescendantNodes() | |
.Where(d => d.Kind == SyntaxKind.MethodDeclaration) | |
.Cast<MethodDeclarationSyntax>().ToList(); | |
methods.Select(z => | |
{ | |
var parameters = z.ParameterList.Parameters.Select(p => p.Identifier.ValueText); | |
return new { MethodName = z.Identifier.ValueText, | |
IsUsingAllParameter = parameters.All(x => z.Body.GetText().ToString().Contains(x))}; | |
}) | |
.Where(x => !x.IsUsingAllParameter) | |
.ToList() | |
.ForEach(x => Console.WriteLine(x.MethodName + " " + x.IsUsingAllParameter)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment