Skip to content

Instantly share code, notes, and snippets.

@sudipto80
Created October 10, 2014 08:38
Show Gist options
  • Save sudipto80/a008f7f992d7a8f01d71 to your computer and use it in GitHub Desktop.
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)
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